首页 > 解决方案 > 在@WebMvcTest 中指定@SpringBootApplication

问题描述

Using将通过查找类(例如)@WebMvcTest来自动配置所有 Web 层 bean 。@SpringBootConfiguration@SpringBootApplication

如果配置类在不同的包中,扫描找不到,可以直接提供给@WebMvcTest吗?

标签: springspring-bootspring-mvcspring-boot-test

解决方案


以下将指向正确的@SpringBootApplication类:

@RunWith(SpringJUnit4ClassRunner.class)
@WebMvcTest(controllers = {MyController.class})
@ContextConfiguration(classes={MySpringBootApplicationClass.class})
public class MyControllerTest {
    //...
}

推荐阅读