Reputation: 2136
I have a REST API written in Java using SpringBoot 2.1.7.
It has 2 controllers and there are integration tests for each controller.
The controllers are in separate files in the same controller folder
The integration tests for each controller are in separate files also. If I comment out 1 set of controller tests, the integration tests are successful. But if I try to run all integration tests for both controllers, there are multiple failures with the same error:
java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class [com.fedex.ground.transportation.fxglhlschedulesvc.controller.ITFacilityController]
java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class [com.fedex.ground.transportation.fxglhlschedulesvc.controller.ITScheduleController]
It seems to be a configuration issue. This is how I have the test files configured: For the Facility Controller
@ActiveProfiles("local")
@AutoConfigureMockMvc
@SpringBootTest(classes = {FxgLhlScheduleSvcApplication.class, RedisConfig.class})
For the Schedule Controller
@ActiveProfiles("local")
@AutoConfigureMockMvc
@SpringBootTest(classes = FxgLhlScheduleSvcApplication.class)
I tried adding these configuration annotations but get the same errors:
@WebMvcTest(ScheduleController.class)
@ContextConfiguration(classes=FxgLhlScheduleSvcApplication.class)
@WebMvcTest(FacilityController.class)
@ContextConfiguration(classes = {FxgLhlScheduleSvcApplication.class, RedisConfig.class})
What are the configuration annotations suppose to be for 2 controllers in separate files. The controllers are not associated with each other at all.
Upvotes: 0
Views: 1042
Reputation: 4161
Integration tests use the same ApplicationContext (unless specifically set not to). The issue with that is that one of the tests can make changes in the context that would affect the other integration tests, like changing state of some beans.
For this reason there is an annotation @DirtiesContext
which restores/cleans the effects on the context after this specific test.
This annotation is computation expensive, therefore you should use it only when necessary.
Upvotes: 2