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
12
votes
5 answers

How to fix this error: java.lang.NoSuchMethodError: 'java.lang.AutoCloseable org.mockito.MockitoAnnotations.openMocks(java.lang.Object)'

So I am getting this error in my Spring boot Gradle project: 'java.lang.AutoCloseable org.mockito.MockitoAnnotations.openMocks(java.lang.Object)' java.lang.NoSuchMethodError: 'java.lang.AutoCloseable…
LardinoisJosse
  • 363
  • 2
  • 5
  • 22
12
votes
4 answers

Overriding @Value in Integration Test

For one of my Spring beans(say Application class), I'm fetching the value of a property(my.property.flag=true/false) from a properties file(prop.properties) using @Value annotation. That works perfectly fine. I need to write an integration test(say…
Sandy
  • 459
  • 2
  • 6
  • 19
12
votes
2 answers

Spring test context best practice

I'm trying to cover a huge Spring Boot application with integration tests. There are lots of Spring beans within the app. It takes a while to load the Spring context. So I'm wondering - Is Spring clever enough to share the same context between…
VB_
  • 45,112
  • 42
  • 145
  • 293
12
votes
4 answers

Check output of JsonPath with Hamcrest Matchers

I wrote Spring controller Junits. I used JsonPath to fetch all IDs from JSON using ["$..id"]. I have following as test method : mockMvc.perform(get(baseURL + "/{Id}/info", ID).session(session)) .andExpect(status().isOk()) // Success …
SuhasD
  • 728
  • 2
  • 7
  • 20
12
votes
2 answers

How to mock Eureka when doing Integration Tests in Spring?

I am running a simple Junit Testing a Controller in Spring Boot. The test code looks like this: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes =…
Keyhan
  • 121
  • 1
  • 1
  • 5
12
votes
4 answers

Aspect not being called in Spring test

I am using Spring 4.16 and i have my ValidationAspect, which validates methods arguments and throws ValidationException if is something wrong. This is being called when i run the server and send requests, but not when comes from the test: package…
jscherman
  • 5,839
  • 14
  • 46
  • 88
12
votes
5 answers

How to load DBUnit test data once per case with Spring Test

Spring Test helpfully rolls back any changes made to the database within a test method. This means that it is not necessary to take the time to delete/reload the test data before each test method. But if you use the @BeforeClass Junit annotation,…
Dave
  • 21,524
  • 28
  • 141
  • 221
12
votes
2 answers

How to run code before SpringJUnit4ClassRunner context initialization?

In my application I initialize a property before spring application startup as follows: MapLookup.setMainArguments(new String[] {"logging.profile", profile}); //from args SpringApplication.run(source, args); (just for reference: it is used for…
membersound
  • 81,582
  • 193
  • 585
  • 1,120
12
votes
2 answers

How to run tests using a custom WebApplicationInitializer?

class MyWebAppInitializer extends WebApplicationInitializer { def onStartup(servletContext: ServletContext): Unit = { ... } } @RunWith(classOf[SpringJUnit4ClassRunner]) @WebAppConfiguration @ContextConfiguration(classes =…
user48956
  • 14,850
  • 19
  • 93
  • 154
12
votes
4 answers

Mock services inside another spring service with mockito

I'm facing problems mocking services injected inside of other services within the Spring framework. Here is my code: @Service("productService") public class ProductServiceImpl implements ProductService { @Autowired private ClientService…
br4zuca
  • 121
  • 1
  • 1
  • 4
12
votes
1 answer

Spring - @Primary fails against @ComponentScan?

For a simple POJO: @Component public class Foo { private final String string; public Foo() { this("Secondary ComponentScan??"); } public Foo(String string) { this.string = string; } @Override …
ManRow
  • 1,563
  • 4
  • 21
  • 40
11
votes
4 answers

@webMvcTest is not excluding and loading beans marked as @Repository

I've a @RestController which has only one dependency in field @Autowire that dependency is @component, that component Class definition has some autowired fields which are @service and those services have some @repositories. In this whole flow I've…
ThrowableException
  • 1,168
  • 1
  • 8
  • 29
11
votes
3 answers

Unknown data type when using an integer over NamedParameterJDBCTemplate on H2 [SPRING-BOOT]

I'm testing a Dao with an In-Memory DB with H2. I'm passing an int to the query with a map to execute it. This query is working OK on Oracle SQL, but is not succeding in H2. DAO @Override public int deleteCancelled(int days) { final…
mariotepro
  • 119
  • 1
  • 2
  • 8
11
votes
6 answers

org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/json;charset=UTF-8' not supported

The error show up when My code like this: @Test public void getTemplateByIdTest() throws Exception { client.get().uri("/template/getTemplate/7") .exchange() .expectStatus().isOk() …
Anderson Feng
  • 121
  • 1
  • 1
  • 4
11
votes
3 answers

Spring Context Test With Just One Bean

What's the recommended way to run a spring boot test where only the one subject under test is configured in the context. If I annotate the test with @RunWith(SpringRunner.class) @SpringBootTest(properties =…
David
  • 7,652
  • 21
  • 60
  • 98