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