SpringBoot運行Test時報錯
運行Test時的報錯信息:SpringBoot Unable to find a @SpringBootConfiguration
錯誤詳情
今天做SpringBoot配置郵件發送的時候,運行測試類,報如下錯誤:
說找不到@SpringBootConfiguration注解,其實是有的,檢查了下啟動類和被測試類的細節,都沒問題,查詢的很多CSDN答案都是互相抄來抄去。。。。比如測試類的包名和啟動類的包名一致等解決辦法,試了都沒用。
解決辦法
原來還要在測試類里制定啟動類。。。解決辦法很簡單,把@SpringBootTest()注解改成@SpringBootTest(classes = App.class)就可以了。就像這樣:
注:我這里的啟動類名為App,更改的時候根據自己啟動類名來改
SpringBootTest單元測試報錯
@RunWith(SpringRunner.class) @SpringBootTest(classes = { DataRulesApplication.class }) @EnableAutoConfiguration //@SpringBootTest(classes = { DataRulesApplication.class }) public class HuaboAddressTest extends AbstractTestNGSpringContextTests { @Autowired private HuaboAddressServiceImpl johnyService; @Test public void queryState() { //johnyService.resetAllDistricts(); long startTime = System.currentTimeMillis(); // johnyService.resetAllDistricts(); // johnyService.batchUpdate2(); // johnyService.batchupdate3(); //johnyService.resetAllDistricts(); johnyService.updateBatch(); long endTime = System.currentTimeMillis(); System.out.println("執行時間:" + (endTime - startTime)); // long startTime = System.currentTimeMillis(); // johnyService.select1(); // long endTime = System.currentTimeMillis(); // System.err.println("執行時間1:"+(endTime-startTime)); // startTime = System.currentTimeMillis(); // johnyService.select2(); // endTime = System.currentTimeMillis(); // System.err.println("執行時間2:"+(endTime-startTime)); } @Test public void check() { } @Test public void register() { } @Test public void detail() { } @Test public void queryCategory() { } }
其實只需要在setting中設置運行test的環境即可。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/x1825048925/article/details/103222867