Spring Boot应用无法运行两个控制器的集成测试。

huangapple go评论73阅读模式
英文:

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
Spring Boot应用无法运行两个控制器的集成测试。

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.

huangapple
  • 本文由 发表于 2020年8月14日 02:29:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63401204.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定