英文:
Spring Boot Application cannot run integration tests for 2 controllers
问题
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 supposed to be for 2 controllers in separate files.
The controllers are not associated with each other at all.
英文:
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.
答案1
得分: 1
集成测试使用相同的应用程序上下文(除非明确设置为不使用)。问题在于,其中一个测试可能会在上下文中进行更改,这会影响其他集成测试,例如更改一些bean的状态。
因此,有一个注释@DirtiesContext
,它可以在特定测试后还原/清除上下文的效果。
这个注释的计算成本很高,因此只有在必要时才应该使用它。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论