上一篇已经成功将spring boot成功启动了,但是测试用例中注入对象全部为空,遇到这种情况怎么办呢?
原来使用testng方法启动的类,并不能直接使用spring boot容器中对象,需要手动去拿,只需要加几行代码即可正常使用了
public static ApplicationContext applicationContext;
public static PreProfitTestCase preProfitTestCase;
public static PreProfitUrl preProfitUrl;
@BeforeClass
public void start(){
if(!Application.started){
applicationContext = SpringApplication.run(Application.class);
Application.started = true;
}else{
applicationContext = Application.get();
}
preProfitUrl = applicationContext.getBean(PreProfitUrl.class);
preProfitTestCase = applicationContext.getBean(PreProfitTestCase.class);
}
[接上一篇]spring boot启动成功之后,测试用例中需要使用的注入对象均为null
原文:https://www.cnblogs.com/biyuting/p/11184353.html