[接上一篇]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