SpringBootTest單元測試報(bào)錯(cuò)的解決方案
SpringBootTest單元測試報(bào)錯(cuò)
@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("執(zhí)行時(shí)間:" + (endTime - startTime)); // long startTime = System.currentTimeMillis(); // johnyService.select1(); // long endTime = System.currentTimeMillis(); // System.err.println("執(zhí)行時(shí)間1:"+(endTime-startTime)); // startTime = System.currentTimeMillis(); // johnyService.select2(); // endTime = System.currentTimeMillis(); // System.err.println("執(zhí)行時(shí)間2:"+(endTime-startTime)); } @Test public void check() { } @Test public void register() { } @Test public void detail() { } @Test public void queryCategory() { } }
其實(shí)只需要在setting中設(shè)置運(yùn)行test的環(huán)境即可。
單元測試 @mock和@SpringBootTest使用
在寫單元測試的過程中我們會發(fā)現(xiàn)需要測試的類有很多依賴,這些依賴的類或者資源又會有依賴,導(dǎo)致在單元測試代碼里無法完成構(gòu)建,我們應(yīng)對的方法是Mock。簡單的說就是模擬這些需要構(gòu)建的類或者資源,提供給需要測試的對象使用。
一.單元測試工具mock使用
1.引入依賴包
2.mock測試類
二.springboot使用@SpringBootTest單元測試
1.引入依賴包
2.測試類
三.mock和@springBootTest區(qū)別
1.mock進(jìn)行單元測試不依賴spring的bean定義文件,不需要啟動web服務(wù),執(zhí)行起來速度很快。
2.@springBootTest需要啟動服務(wù),執(zhí)行真正的操作,執(zhí)行速度慢,當(dāng)需要真正的dao層操作時(shí)可選此測試方式。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
一文帶你吃透JSP增刪改查實(shí)戰(zhàn)案例詳細(xì)解讀
這篇文章主要為大家詳細(xì)介紹了JSP中增刪改查實(shí)戰(zhàn)案例的相關(guān)知識,文中的示例代碼講解現(xiàn)象,具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解一下2023-03-03