[接上一篇]spring boot启动成功之后,测试用例中需要使用的注入对象均为null


上一篇已经成功将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);
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM