使用@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 {
    ...
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM