首页 > 编程语言 > 详细

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

时间:2019-07-14 16:03:03      阅读:209      评论:0      收藏:0      [点我收藏+]

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!