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)文章
基于Springboot使用logback的注意事項(xiàng)
這篇文章主要介紹了Springboot使用logback的注意事項(xiàng),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Java集合中獲取數(shù)據(jù)前驅(qū)和后繼元素的實(shí)現(xiàn)
使用一致性hash時(shí),如何找到一個(gè)hash值對應(yīng)的臨近節(jié)點(diǎn),可以使用集合中獲取數(shù)據(jù)的前驅(qū)和后繼元素實(shí)現(xiàn),所以本文給大家介紹了Java集合中獲取數(shù)據(jù)前驅(qū)和后繼元素的實(shí)現(xiàn),文中有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下2024-05-05
Springboot項(xiàng)目中使用redis的配置詳解
這篇文章主要介紹了Springboot項(xiàng)目中使用redis的配置詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
IDEA安裝lombok插件設(shè)置Enable Annotation Processing后編譯依然報(bào)錯(cuò)解決方法
這篇文章主要介紹了IDEA安裝lombok插件設(shè)置Enable Annotation Processing后編譯依然報(bào)錯(cuò)解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Java函數(shù)式開發(fā) Optional空指針處理
本文主要介紹Java函數(shù)式開發(fā) Optional空指針處理,這里整理了相關(guān)資料,及示例代碼,有興趣的小伙伴可以參考下2016-09-09
修改SpringBoot啟動圖標(biāo)banner的兩種方式
Banner即橫幅標(biāo)語,我們在啟動SpringBoot項(xiàng)目時(shí)會將Banner信息打印至控制臺,我們可以輸出一些圖形、SpringBoot版本信息等內(nèi)容,有很多小伙伴想知道如何修改SpringBoot啟動圖標(biāo)banner,接下來由小編給大家介紹一下吧2024-08-08

