? ? ? ? 單元測(cè)試不僅能幫我們驗(yàn)證代碼邏輯,還可以通過(guò)執(zhí)行時(shí)間進(jìn)行快速篩選不合格代碼并進(jìn)行優(yōu)化。核心模塊的代碼的單元測(cè)試覆蓋率要達(dá)到100%,其他模塊代碼的單元測(cè)試覆蓋率需要達(dá)到60%。下面將介紹在IDEA上使用Junit4進(jìn)行單元測(cè)試的方法:
一、安裝JunitGenerator2插件
? ? ? ? 如下圖所示,file - setting - plugin - 搜索JunitGenerator2插件, 安裝插件后重啟IDEA
?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-414658.html
二、配置JunitGenerator2
- Outpun Path: 修改為
${SOURCEPATH}/../../test/java/${PACKAGE}/${FILENAME}
- ? Default Template: Junit 3 -> Junit4
- ?配置Junit4模板,如下圖所示 ,注意:需要使用spring容器管理的Bean,需要在測(cè)試類增加 @SpringbootTest 與 @Runwith(SpringRunner.class)
?
?三、pom文件引入Junit依賴
? ? 如果不引入依賴,無(wú)法使用Junit工具!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-414658.html
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
?四、編寫測(cè)試代碼
- 打開(kāi)需要生成單元測(cè)試的類,alt+insert插入測(cè)試類
- 編寫測(cè)試代碼
到了這里,關(guān)于IDEA使用Junit4進(jìn)行單元測(cè)試的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!