SpringBootTest單元測試報錯的解決方案
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("執(zhí)行時間:" + (endTime - startTime));
// long startTime = System.currentTimeMillis();
// johnyService.select1();
// long endTime = System.currentTimeMillis();
// System.err.println("執(zhí)行時間1:"+(endTime-startTime));
// startTime = System.currentTimeMillis();
// johnyService.select2();
// endTime = System.currentTimeMillis();
// System.err.println("執(zhí)行時間2:"+(endTime-startTime));
}
@Test
public void check() {
}
@Test
public void register() {
}
@Test
public void detail() {
}
@Test
public void queryCategory() {
}
}

其實只需要在setting中設置運行test的環(huán)境即可。

單元測試 @mock和@SpringBootTest使用
在寫單元測試的過程中我們會發(fā)現(xiàn)需要測試的類有很多依賴,這些依賴的類或者資源又會有依賴,導致在單元測試代碼里無法完成構(gòu)建,我們應對的方法是Mock。簡單的說就是模擬這些需要構(gòu)建的類或者資源,提供給需要測試的對象使用。
一.單元測試工具mock使用
1.引入依賴包

2.mock測試類

二.springboot使用@SpringBootTest單元測試
1.引入依賴包

2.測試類

三.mock和@springBootTest區(qū)別
1.mock進行單元測試不依賴spring的bean定義文件,不需要啟動web服務,執(zhí)行起來速度很快。
2.@springBootTest需要啟動服務,執(zhí)行真正的操作,執(zhí)行速度慢,當需要真正的dao層操作時可選此測試方式。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java集合中獲取數(shù)據(jù)前驅(qū)和后繼元素的實現(xiàn)
使用一致性hash時,如何找到一個hash值對應的臨近節(jié)點,可以使用集合中獲取數(shù)據(jù)的前驅(qū)和后繼元素實現(xiàn),所以本文給大家介紹了Java集合中獲取數(shù)據(jù)前驅(qū)和后繼元素的實現(xiàn),文中有相關的代碼示例供大家參考,需要的朋友可以參考下2024-05-05
IDEA安裝lombok插件設置Enable Annotation Processing后編譯依然報錯解決方法
這篇文章主要介紹了IDEA安裝lombok插件設置Enable Annotation Processing后編譯依然報錯解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04
Java函數(shù)式開發(fā) Optional空指針處理
本文主要介紹Java函數(shù)式開發(fā) Optional空指針處理,這里整理了相關資料,及示例代碼,有興趣的小伙伴可以參考下2016-09-09

