java.lang.IllegalStateException:配置错误:为测试类找到多个@BootstrapWith声明
我有一个SpringBoot和RESTAPI项目。我正在测试findAll@GET操作。下面是显示所有记录方法的测试用例java.lang.IllegalStateException:配置错误:为测试类找到多个@BootstrapWith声明,java,rest,spring-boot,junit4,Java,Rest,Spring Boot,Junit4,我有一个SpringBoot和RESTAPI项目。我正在测试findAll@GET操作。下面是显示所有记录方法的测试用例 @Before public void setUp() throws Exception { mockMvc = MockMvcBuilders.standaloneSetup(batchJobConfigController).build(); } @Test public void testBatchJobCo
@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]:
当spring测试找不到主配置类时,会发生此异常。尝试将@ContextConfiguration注释添加到测试类中 e、 g
添加@ContextConfiguration注释并定义包含包名的类解决了此问题。 @ContextConfiguration(classes=com.somepath.pack.Application.class)@RunWith(SpringRunner.class)@springbootest@WebMvcTest(value=BatchJobConfigController.class,secure=false)公共类BatchJobConfigControllerTest{//此处的上述方法………..]
@RunWith(SpringRunner.class)
@ContextConfiguration(classes=Application.class)
@WebMvcTest(MyController.class)
public class MyConrollerTests {
...
}