簡體   English   中英

java.lang.IllegalStateException:配置錯誤:為測試類找到了@BootstrapWith的多個聲明

[英]java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class

我有spring-boot和REST API項目。 我正在嘗試測試findAll @GET操作。 下面的測試用例為顯示所有記錄的方法。

  @Before
        public void setUp() throws Exception {
            mockMvc = MockMvcBuilders.standaloneSetup(batchJobConfigController).build();
        }

@Test
public void testBatchJobConfigs() throws Exception {
    BatchJobConfigDTO mockBatchJobConfigDTO = new BatchJobConfigDTO("Doctor", "ER Doctor", "Started", "Full Time");

    batchJobConfigDTOs.add(mockBatchJobConfigDTO);

    when(mockBatchJobConfigService.findAllBatchJobConfigs()).thenReturn(batchJobConfigDTOs);
    mockMvc.perform(get("/configs").accept(MediaType.APPLICATION_JSON))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.jsonPath("$.jobNm", Matchers.is("Enginerring")))
            .andExpect(MockMvcResultMatchers.jsonPath("$.jobDesc", Matchers.is("Coding, Testing and stuff")))
            .andExpect(MockMvcResultMatchers.jsonPath("$.status", Matchers.is("Progress")))
            .andExpect(MockMvcResultMatchers.jsonPath("$.jobType", Matchers.is("INFA")));
    verify(mockBatchJobConfigService, times(1)).findAllBatchJobConfigs();
    verifyNoMoreInteractions(mockBatchJobConfigService);

}

我正在JUnit4中運行以下程序。 可能是什么原因?

java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class [com.controller.BatchJobConfigControllerTest]: 

當彈簧測試找不到主配置類時,將發生此異常。 嘗試將@ContextConfiguration批注添加到測試類。

例如

@RunWith(SpringRunner.class)
@ContextConfiguration(classes=Application.class)
@WebMvcTest(MyController.class)
public class MyConrollerTests {
    ...
}

添加@ContextConfiguration批注並定義包含程序包名稱的類可解決此問題。 @ContextConfiguration(classes = com.somepath.pack.Application.class)

暫無
暫無

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

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