在Spring boot的項(xiàng)目中使用Junit進(jìn)行單體測(cè)試
使用Junit或者TestNG可以進(jìn)行單體測(cè)試,這篇文章簡(jiǎn)單說(shuō)明一下如何在Spring boot的項(xiàng)目中使用Junit進(jìn)行單體測(cè)試。
pom設(shè)定
pom中需要添加spring-boot-starter-test
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
確認(rèn)依賴
使用Java進(jìn)行Unit測(cè)試至少需要Junit之類的測(cè)試框架,另外spring boot相關(guān)的測(cè)試還應(yīng)該有一些Mock相關(guān)的依賴,這個(gè)spring-boot-starter-test到底包含什么,可以使用maven dependency來(lái)進(jìn)行確認(rèn)一下。
使用命令:mvn dependency:tree
結(jié)果信息:
[INFO] --- maven-dependency-plugin:3.0.2:tree (default-cli) @ springbootdemo --- [INFO] com.liumiaocn:springbootdemo:jar:0.0.1-SNAPSHOT [INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.0.6.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter:jar:2.0.6.RELEASE:compile [INFO] | | +- org.springframework.boot:spring-boot:jar:2.0.6.RELEASE:compile [INFO] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.0.6.RELEASE:compile [INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.0.6.RELEASE:compile [INFO] | | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile [INFO] | | | | \- ch.qos.logback:logback-core:jar:1.2.3:compile [INFO] | | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.10.0:compile [INFO] | | | | \- org.apache.logging.log4j:log4j-api:jar:2.10.0:compile [INFO] | | | \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile [INFO] | | +- javax.annotation:javax.annotation-api:jar:1.3.2:compile [INFO] | | \- org.yaml:snakeyaml:jar:1.19:runtime [INFO] | +- org.springframework.boot:spring-boot-starter-json:jar:2.0.6.RELEASE:compile [INFO] | | +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.7:compile [INFO] | | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile [INFO] | | | \- com.fasterxml.jackson.core:jackson-core:jar:2.9.7:compile [INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.7:compile [INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.7:compile [INFO] | | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.7:compile [INFO] | +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.0.6.RELEASE:compile [INFO] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.34:compile [INFO] | | +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.34:compile [INFO] | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.34:compile [INFO] | +- org.hibernate.validator:hibernate-validator:jar:6.0.13.Final:compile [INFO] | | +- javax.validation:validation-api:jar:2.0.1.Final:compile [INFO] | | +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile [INFO] | | \- com.fasterxml:classmate:jar:1.3.4:compile [INFO] | +- org.springframework:spring-web:jar:5.0.10.RELEASE:compile [INFO] | | \- org.springframework:spring-beans:jar:5.0.10.RELEASE:compile [INFO] | \- org.springframework:spring-webmvc:jar:5.0.10.RELEASE:compile [INFO] | +- org.springframework:spring-aop:jar:5.0.10.RELEASE:compile [INFO] | +- org.springframework:spring-context:jar:5.0.10.RELEASE:compile [INFO] | \- org.springframework:spring-expression:jar:5.0.10.RELEASE:compile [INFO] \- org.springframework.boot:spring-boot-starter-test:jar:2.0.6.RELEASE:test [INFO] +- org.springframework.boot:spring-boot-test:jar:2.0.6.RELEASE:test [INFO] +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.0.6.RELEASE:test [INFO] +- com.jayway.jsonpath:json-path:jar:2.4.0:test [INFO] | +- net.minidev:json-smart:jar:2.3:test [INFO] | | \- net.minidev:accessors-smart:jar:1.2:test [INFO] | | \- org.ow2.asm:asm:jar:5.0.4:test [INFO] | \- org.slf4j:slf4j-api:jar:1.7.25:compile [INFO] +- junit:junit:jar:4.12:test [INFO] +- org.assertj:assertj-core:jar:3.9.1:test [INFO] +- org.mockito:mockito-core:jar:2.15.0:test [INFO] | +- net.bytebuddy:byte-buddy:jar:1.7.11:test [INFO] | +- net.bytebuddy:byte-buddy-agent:jar:1.7.11:test [INFO] | \- org.objenesis:objenesis:jar:2.6:test [INFO] +- org.hamcrest:hamcrest-core:jar:1.3:test [INFO] +- org.hamcrest:hamcrest-library:jar:1.3:test [INFO] +- org.skyscreamer:jsonassert:jar:1.5.0:test [INFO] | \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test [INFO] +- org.springframework:spring-core:jar:5.0.10.RELEASE:compile [INFO] | \- org.springframework:spring-jcl:jar:5.0.10.RELEASE:compile [INFO] +- org.springframework:spring-test:jar:5.0.10.RELEASE:test [INFO] \- org.xmlunit:xmlunit-core:jar:2.5.1:test
在org.springframework.boot:spring-boot-starter-test:jar:2.0.6.RELEASE:test下面我們看到了junit:junit:jar:4.12:test以及org.mockito:mockito-core:jar:2.15.0:test的信息,這是spring boot已經(jīng)整理完畢的內(nèi)容,我們只需要寫(xiě)TestCase即可。
創(chuàng)建測(cè)試目錄
根據(jù)慣例創(chuàng)建測(cè)試目錄如下:
liumiaocn:src liumiao$ ls main test liumiaocn:src liumiao$ find . -type d . ./test ./test/java ./test/java/com ./test/java/com/liumiaocn ./test/java/com/liumiaocn/springbootdemo ./main ./main/resources ./main/java ./main/java/com ./main/java/com/liumiaocn ./main/java/com/liumiaocn/springbootdemo liumiaocn:src liumiao$
創(chuàng)建測(cè)試用例
代碼示例
liumiaocn:src liumiao$ cat ./test/java/com/liumiaocn/springbootdemo/SpringbootdemoApplicationTests.java package com.liumiaocn.springbootdemo; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class SpringbootdemoApplicationTests { @Test public void contextLoads() { } } liumiaocn:src liumiao$
代碼說(shuō)明
- COC:根據(jù)慣例進(jìn)行定義測(cè)試類的名稱
- SpringBootTest注解:SpringBootTest是1.4之后引入的一個(gè)注解,使得springboot的測(cè)試變得更加方便
- RunWith注解:使用了此注解的情況下,JUnit會(huì)調(diào)用RunWith中所指定的類。不同的框架提供相應(yīng)的Runner用于測(cè)試,比如Junit自己的JUnit4.class,在比如spring的SpringJUnit4ClassRunner或者SpringRunner,都可與之結(jié)合使用。
- Test注解:Junit的常用注解之一,用于定義測(cè)試方法,不再贅述。
執(zhí)行測(cè)試
命令: mvn test
部分執(zhí)行內(nèi)容如下:
[INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
在實(shí)施的結(jié)果中可以看到運(yùn)行起來(lái)的測(cè)試用例為1個(gè),沒(méi)有失敗/跳過(guò)/出錯(cuò)的。
小結(jié)
通過(guò)spring-boot-starter-test的引入,在springboot中已經(jīng)做好所有單元測(cè)試的準(zhǔn)備,根據(jù)慣例的方式設(shè)定測(cè)試目錄和文件名稱,結(jié)合使用springboottest等注解,可以使用Junit對(duì)springboot的應(yīng)用進(jìn)行測(cè)試。最后使用mvn test即可運(yùn)行相關(guān)的測(cè)試用例并可確認(rèn)結(jié)果,后面將會(huì)進(jìn)一步說(shuō)明如何與sonarqube以及jacoco等進(jìn)行結(jié)合確認(rèn)代碼掃描等。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
java:程序包org.springframework.boot不存在的完美解決方法
最近項(xiàng)目中運(yùn)行的時(shí)候提示了"java: 程序包org.springframework.boot不存在",下面這篇文章主要給大家介紹了關(guān)于java:程序包org.springframework.boot不存在的完美解決方法,需要的朋友可以參考下2023-05-05SpringCloud微服務(wù)應(yīng)用config配置中心詳解
這篇文章主要介紹了SpringCloud微服務(wù)應(yīng)用-config配置中心,包括相關(guān)知識(shí)介紹、搭建、動(dòng)態(tài)刷新、測(cè)試,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07Spring通過(guò)三級(jí)緩存解決循環(huán)依賴問(wèn)題的過(guò)程詳解
循環(huán)依賴指的是在對(duì)象之間存在相互依賴關(guān)系,形成一個(gè)閉環(huán),導(dǎo)致無(wú)法準(zhǔn)確地完成對(duì)象的創(chuàng)建和初始化,本文主要介紹了Spring通過(guò)三級(jí)緩存解決循環(huán)依賴的方法,需要的可以參考下2023-10-10IDEA 去除 mybatis.xml 文件黃色警告的圖文教程
這篇文章主要介紹了IDEA 去除 mybatis.xml 文件黃色警告的方法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07Java Spring MVC 上傳下載文件配置及controller方法詳解
這篇文章主要介紹了Java Spring MVC 上傳下載文件配置及controller方法詳解,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09詳解SpringBoot啟動(dòng)類的掃描注解的用法及沖突原則
這篇文章主要介紹了詳解SpringBoot啟動(dòng)類的掃描注解的用法及沖突原則,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11