所以我有一个多 Maven 模块项目,其中有一个用于控制器的模块和一个包含 Spring Boot 启动应用程序类的模块。
myApp - 部署(模块) - 控制器(模块)
在控制器模块中,我希望能够使用 mockMvc 测试它们
但是,当我运行它时,出现以下错误:
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
我想要做的是仍然能够测试这个,我可以创建一个测试应用程序并使用它吗?
@RunWith(SpringRunner.class)
@WebMvcTest(Controller.class)
@ActiveProfiles(Constants.TEST)
public class ControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void contextLoads() {
assertNotNull(mockMvc);
}
}
您可以在测试包中创建一个“测试配置”类,如下所示:
@SpringBootApplication
public class TestConfig {
}
我正在使用这个方法来处理我需要测试的自定义存储库或服务 libe
比你可以用
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
而且比你能
@Autowired
protected MockMvc mockMvc;
以及您需要的所有其他东西。
希望这可以帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句