SpringBoot 单元测试忽略@component注解

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

例如以下代码:

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

服务启动会执行commandLineRanner实例。那如何忽略commandLineRanner实例这个@component呢?
其实很简单,在commandLineRanner实例上加注解@TestConfiguration或者@TestComponent就可以忽略。

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

原文地址:https://www.cnblogs.com/gmhappy/p/11863956.html