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的環境即可。
單元測試 @mock和@SpringBootTest使用
在寫單元測試的過程中我們會發現需要測試的類有很多依賴,這些依賴的類或者資源又會有依賴,導致在單元測試代碼里無法完成構建,我們應對的方法是Mock。簡單的說就是模擬這些需要構建的類或者資源,提供給需要測試的對象使用。
一.單元測試工具mock使用
1.引入依賴包
2.mock測試類
二.springboot使用@SpringBootTest單元測試
1.引入依賴包
2.測試類
三.mock和@springBootTest區別
1.mock進行單元測試不依賴spring的bean定義文件,不需要啟動web服務,執行起來速度很快。
2.@springBootTest需要啟動服務,執行真正的操作,執行速度慢,當需要真正的dao層操作時可選此測試方式。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/lin451791119/article/details/106409812