简体   繁体   English

Spring 引导应用程序无法运行 2 个控制器的集成测试

[英]Spring Boot Application cannot run integration tests for 2 controllers

I have a REST API written in Java using SpringBoot 2.1.7.我有一个使用 SpringBoot 2.1.7 用 Java 编写的 REST API。 It has 2 controllers and there are integration tests for each controller.它有 2 个控制器,每个 controller 都有集成测试。 The controllers are in separate files in the same controller folder控制器位于同一 controller 文件夹中的单独文件中在此处输入图像描述

The integration tests for each controller are in separate files also.每个 controller 的集成测试也在单独的文件中。 If I comment out 1 set of controller tests, the integration tests are successful.如果我注释掉一组 controller 测试,则集成测试成功。 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. java.lang.IllegalStateException:配置错误:发现@BootstrapWith 的多个声明用于测试 class [com.fedex.ground.transportation.fxglhlschedulesvc.Z594C103F2C6E01E0C1DAZ.059F0E04C1DAZ. ITFacilityController ] ITF 设施控制器]

java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class [com.fedex.ground.transportation.fxglhlschedulesvc.controller. java.lang.IllegalStateException:配置错误:发现@BootstrapWith 的多个声明用于测试 class [com.fedex.ground.transportation.fxglhlschedulesvc.Z594C103F2C6E01E0C1DAZ.059F0E04C1DAZ. ITScheduleController ]它调度控制器]

It seems to be a configuration issue.这似乎是一个配置问题。 This is how I have the test files configured: For the Facility Controller这就是我配置测试文件的方式:对于设施 Controller

@ActiveProfiles("local")
@AutoConfigureMockMvc
@SpringBootTest(classes = {FxgLhlScheduleSvcApplication.class, RedisConfig.class})

For the Schedule Controller对于时间表 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.对于单独文件中的 2 个控制器,配置注释应该是什么。 The controllers are not associated with each other at all.控制器之间根本没有关联。

Integration tests use the same ApplicationContext (unless specifically set not to).集成测试使用相同的 ApplicationContext(除非特别设置为不使用)。 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.问题在于其中一个测试可以在上下文中进行更改,这会影响其他集成测试,例如更改某些 bean 的 state。

For this reason there is an annotation @DirtiesContext which restores/cleans the effects on the context after this specific test.出于这个原因,有一个注释@DirtiesContext在这个特定的测试之后恢复/清除对上下文的影响。

This annotation is computation expensive, therefore you should use it only when necessary.此注释的计算成本很高,因此您应该仅在必要时使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM