【发布时间】:2019-07-16 13:34:11
【问题描述】:
所以我有一个多 maven 模块项目,其中包含一个用于控制器的模块和一个包含 spring boot 启动应用程序类的模块。
我的应用 - 部署(模块) - 控制器(模块)
在控制器模块中,我希望能够使用 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,就像普通类在你的模块测试包下有main函数一样
标签: spring-boot spring-test-mvc