使用@ContextConfiguration替换@SpringBootTest

由于springboottest需要初始化上下文,每一次都要好长时间,可以使用其他方法替换它:

测试类如下:

@RunWith(SpringJUnit4ClassRunner.class)
// @WebAppConfiguration omitted on purpose
@ContextConfiguration(classes = WebConfig.class)
public class EmployeeTestWithoutMockMvc {
 
    @Autowired
    private EmployeeController employeeController;
 
    ...
}

 

WebConfig.java类:

@enableautoconfiguration
@enablejparepository
@entityscan("com.baeldung.domain")
@ComponentScan("com.baeldung.web")
public class WebConfig implements WebMvcConfigurer {
    ...
}

 

posted @ 2020-01-05 20:54  ppjj  阅读(2247)  评论(0编辑  收藏  举报