Questions tagged [spring-test]

`spring-test` is the testing module of the Spring Framework, providing support for unit and integration testing with JUnit and TestNG, including various mocks for unit testing and the Spring TestContext Framework & the Spring MVC Test Framework for integration testing Spring-based applications.

spring-test is the testing module of the core Spring Framework, providing support for and and based applications with and , including mocks for things like the and APIs, the Spring TestContext Framework, and the Spring MVC Test Framework.

Online Resources:

Related Tags:

2041 questions
62
votes
4 answers

spring boot test unable to inject TestRestTemplate and MockMvc

I am using spring boot 1.4.0.RELEASE. I am writing tests for my controller class. I get the following exception. org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name…
brain storm
  • 30,124
  • 69
  • 225
  • 393
61
votes
10 answers

Spring boot 1.4 Testing : Configuration error: found multiple declarations of @BootstrapWith

By following the official doc here: http://docs.spring.io/spring-boot/docs/1.4.0.M2/reference/htmlsingle/#Testing i wanted to test one of my REST API method like…
lesnar
  • 2,400
  • 7
  • 41
  • 72
61
votes
3 answers

Override a single @Configuration class on every spring boot @Test

On my spring boot application I want to override just one of my @Configuration classes with a test configuration (in particular my @EnableAuthorizationServer @Configuration class), on all of my tests. So far after an overview of spring boot testing…
NotGaeL
  • 8,344
  • 5
  • 40
  • 70
61
votes
6 answers

How to clean up mocks in spring tests when using Mockito

I used to use JMock2 for unit tests. As far as I know, JMock2 preserves the expectations and other mock information in a context which will be rebuilt for every test method. Therefore every test method is not interfered by the others. I adopted the…
Yugang Zhou
  • 7,123
  • 6
  • 32
  • 60
58
votes
6 answers

How to autowire field in static @BeforeClass?

@RunWith(SpringJUnit4ClassRunner.class) public void ITest { @Autowired private EntityRepository dao; @BeforeClass public static void init() { dao.save(initialEntity); //not possible as field is not static } } How can I…
membersound
  • 81,582
  • 193
  • 585
  • 1,120
57
votes
5 answers

@RunWith(SpringRunner.class) vs @RunWith(MockitoJUnitRunner.class)

I was using @RunWith(MockitoJUnitRunner.class) for my junit test with mockito. But now i am working with spring boot app and trying to use @RunWith(SpringRunner.class) . Does using @RunWith(SpringRunner.class) has any advantages over using…
user8710021
56
votes
8 answers

Configure specific in memory database for testing purpose in Spring

How do I configure my Spring Boot application so that when I run unit tests it will use in-memory database such as H2/HSQL but when I run Spring Boot application it will use production database PostgreSQL/MySQL ?
IllSc
  • 1,419
  • 3
  • 17
  • 24
56
votes
4 answers

java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling

I have the following test class: @ActiveProfiles({ "DataTC", "test" }) @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {BaseTestConfiguration.class, DataTestConfiguration.class, JpaConfiguration.class,…
balteo
  • 23,602
  • 63
  • 219
  • 412
54
votes
13 answers

Spring beans redefinition in unit test environment

We are using Spring for my application purposes, and Spring Testing framework for unit tests. We have a small problem though: the application code loads a Spring application context from a list of locations (XML files) in the classpath. But when we…
Stas
  • 1,059
  • 2
  • 16
  • 26
53
votes
7 answers

How to unit test a Spring MVC controller using @PathVariable?

I have a simple annotated controller similar to this one: @Controller public class MyController { @RequestMapping("/{id}.html") public String doSomething(@PathVariable String id, Model model) { // do something return "view"; } } and I…
martiner
  • 674
  • 1
  • 6
  • 10
52
votes
1 answer

What is difference between @SpyBean and @MockBean in Mockito?

What is the difference between the @SpyBean and @MockBean annotations in Spring? I have already gone through the JavaDoc but didn't get the difference. If possible please give an example when to use MockBean and when SpyBean.
Vikrant Chaudhary
  • 629
  • 1
  • 7
  • 12
48
votes
6 answers

How to mock remote REST API in unit test with Spring?

Assume I have made a simple client in my application that uses a remote web service that is exposing a RESTful API at some URI /foo/bar/{baz}. Now I wish to unit test my client that makes calls to this web service. Ideally, in my tests, I’d like to…
user1019830
46
votes
9 answers

How to write Junit test for mapstruct abstract mapper injected via Spring

I'm using MapStruct, mapstruct-jdk8 version 1.1.0.Final and defining abstract class that I inject via Spring. I'm looking at how to be able to test them via Junit Test ? I've basicaly a main mapper that will use 2 sub mappers @Mapper(componentModel…
TheBakker
  • 2,852
  • 2
  • 28
  • 49
45
votes
10 answers

MockMvc no longer handles UTF-8 characters with Spring Boot 2.2.0.RELEASE

After I upgraded to the newly released 2.2.0.RELEASE version of Spring Boot some of my tests failed. It appears that the MediaType.APPLICATION_JSON_UTF8 has been deprecated and is no longer returned as default content type from controller methods…
times29
  • 2,782
  • 2
  • 21
  • 40
43
votes
3 answers

Spring MockMvc verify body is empty

I have a simple Spring test @Test public void getAllUsers_AsPublic() throws Exception { doGet("/api/users").andExpect(status().isForbidden()); } public ResultActions doGet(String url) throws Exception { return…
isADon
  • 3,433
  • 11
  • 35
  • 49