SpringBoot 单元测试忽略@component注解

90哦吼

关注

阅读 71

2022-02-13


​springboot​​​框架在单元测试时可能需要忽略某些带有​​@component​​的实例

例如以下代码:

@Component
public class MyCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... var1) throws Exception {
}
}

服务启动会执行​​commandLineRanner​​实例。那如何忽略​​commandLineRanner​​实例这个​​@component​​呢?

其实很简单,在​​commandLineRanne​​r实例上加注解​​@TestConfiguration​​或者​​@TestComponent​​就可以忽略。

官方文档链接:​​http://docs.spring.io/spring-boot/docs/1.5.1.RELEASE/reference/htmlsingle/#boot-features-testing​​



精彩评论(0)

0 0 举报