簡體   English   中英

Spring 引導應用程序無法運行 2 個控制器的集成測試

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

我有一個使用 SpringBoot 2.1.7 用 Java 編寫的 REST API。 它有 2 個控制器,每個 controller 都有集成測試。 控制器位於同一 controller 文件夾中的單獨文件中在此處輸入圖像描述

每個 controller 的集成測試也在單獨的文件中。 如果我注釋掉一組 controller 測試,則集成測試成功。 但是,如果我嘗試為兩個控制器運行所有集成測試,則會出現多個故障並出現相同的錯誤:

java.lang.IllegalStateException:配置錯誤:發現@BootstrapWith 的多個聲明用於測試 class [com.fedex.ground.transportation.fxglhlschedulesvc.Z594C103F2C6E01E0C1DAZ.059F0E04C1DAZ. ITF 設施控制器]

java.lang.IllegalStateException:配置錯誤:發現@BootstrapWith 的多個聲明用於測試 class [com.fedex.ground.transportation.fxglhlschedulesvc.Z594C103F2C6E01E0C1DAZ.059F0E04C1DAZ. 它調度控制器]

這似乎是一個配置問題。 這就是我配置測試文件的方式:對於設施 Controller

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

對於時間表 Controller

@ActiveProfiles("local")
@AutoConfigureMockMvc
@SpringBootTest(classes = FxgLhlScheduleSvcApplication.class)

我嘗試添加這些配置注釋,但得到相同的錯誤:

@WebMvcTest(ScheduleController.class)
@ContextConfiguration(classes=FxgLhlScheduleSvcApplication.class)

@WebMvcTest(FacilityController.class)
@ContextConfiguration(classes = {FxgLhlScheduleSvcApplication.class, RedisConfig.class})

對於單獨文件中的 2 個控制器,配置注釋應該是什么。 控制器之間根本沒有關聯。

集成測試使用相同的 ApplicationContext(除非特別設置為不使用)。 問題在於其中一個測試可以在上下文中進行更改,這會影響其他集成測試,例如更改某些 bean 的 state。

出於這個原因,有一個注釋@DirtiesContext在這個特定的測試之后恢復/清除對上下文的影響。

此注釋的計算成本很高,因此您應該僅在必要時使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM