【问题标题】:java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test classjava.lang.IllegalStateException: 配置错误: 为测试类找到多个@BootstrapWith 声明
【发布时间】:2019-04-19 17:40:02
【问题描述】:

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

【问题讨论】:

  • @RunWith(SpringRunner.class) @SpringBootTest @WebMvcTest(value = BatchJobConfigController.class, secure = false) public class BatchJobConfigControllerTest {//上面的方法在这里............ .......}

标签: java rest spring-boot junit4


【解决方案1】:

spring test 找不到主配置类时会出现此异常。尝试将 @ContextConfiguration 注释添加到您的测试类。

例如

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2020-04-13
      • 2019-10-16
      • 2017-04-24
      • 2020-08-06
      • 1970-01-01
      • 2018-08-06
      • 2020-11-01
      • 2012-04-11
      • 1970-01-01
      相关资源
      最近更新 更多