java.lang.IllegalStateException:配置错误:为测试类找到了@BootstrapWith的多个声明

时间:2019-04-19 17:40:03

标签: java rest spring-boot junit4

我有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]: 

2 个答案:

答案 0 :(得分:0)

当弹簧测试找不到主配置类时,将发生此异常。尝试将@ContextConfiguration批注添加到测试类。

例如

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

答案 1 :(得分:0)

添加@ContextConfiguration批注并定义包含包名称的类解决了此问题。 @ContextConfiguration(classes = com.somepath.pack.Application.class)