SpringBootTest單元測(cè)試報(bào)錯(cuò)的解決方案
SpringBootTest單元測(cè)試報(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)境即可。
單元測(cè)試 @mock和@SpringBootTest使用
在寫(xiě)單元測(cè)試的過(guò)程中我們會(huì)發(fā)現(xiàn)需要測(cè)試的類(lèi)有很多依賴(lài),這些依賴(lài)的類(lèi)或者資源又會(huì)有依賴(lài),導(dǎo)致在單元測(cè)試代碼里無(wú)法完成構(gòu)建,我們應(yīng)對(duì)的方法是Mock。簡(jiǎn)單的說(shuō)就是模擬這些需要構(gòu)建的類(lèi)或者資源,提供給需要測(cè)試的對(duì)象使用。
一.單元測(cè)試工具mock使用
1.引入依賴(lài)包
2.mock測(cè)試類(lèi)
二.springboot使用@SpringBootTest單元測(cè)試
1.引入依賴(lài)包
2.測(cè)試類(lèi)
三.mock和@springBootTest區(qū)別
1.mock進(jìn)行單元測(cè)試不依賴(lài)spring的bean定義文件,不需要啟動(dòng)web服務(wù),執(zhí)行起來(lái)速度很快。
2.@springBootTest需要啟動(dòng)服務(wù),執(zhí)行真正的操作,執(zhí)行速度慢,當(dāng)需要真正的dao層操作時(shí)可選此測(cè)試方式。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring入門(mén)基礎(chǔ)之依賴(lài)注入
Idea中使用@Autowire注解會(huì)出現(xiàn)提示黃線,強(qiáng)迫癥患者看著很難受,使用構(gòu)造器注入或者setter方法注入后可解決,下面我們一起來(lái)看看2022-07-07一文帶你吃透JSP增刪改查實(shí)戰(zhàn)案例詳細(xì)解讀
這篇文章主要為大家詳細(xì)介紹了JSP中增刪改查實(shí)戰(zhàn)案例的相關(guān)知識(shí),文中的示例代碼講解現(xiàn)象,具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解一下2023-03-03java內(nèi)部類(lèi)的那些事兒_讓你一看就弄明白
本篇文章介紹了,java內(nèi)部類(lèi)的那些事兒。需要的朋友參考下2013-05-05