https://github.com/zq2599/blog_demosjava
內容:全部原創文章分類彙總及配套源碼,涉及Java、Docker、Kubernetes、DevOPS等;git
《JUnit5學習》系列旨在經過實戰提高SpringBoot環境下的單元測試技能,一共八篇文章,連接以下:程序員
本文是《JUnit5學習》系列的第五篇,一塊兒來學習JUnit5的標籤(Tag)功能,設想一個工程中的有不少測試類和測試方法,有的場景只需執行其中一部分測試方法,如何實現呢?此時Junit的標籤功能就派上用場了,我們能夠按須要給測試類或者方法打標籤,在執行單元測試時按照標籤進行過濾,學完了標籤再來了解JUnit5對自定義註解的支持狀況,本篇大綱以下:github
名稱 | 連接 | 備註 |
---|---|---|
項目主頁 | https://github.com/zq2599/blog_demos | 該項目在GitHub上的主頁 |
git倉庫地址(https) | https://github.com/zq2599/blog_demos.git | 該項目源碼的倉庫地址,https協議 |
git倉庫地址(ssh) | git@github.com:zq2599/blog_demos.git | 該項目源碼的倉庫地址,ssh協議 |
package com.bolingcavalry.tag.service.impl; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import static org.junit.jupiter.api.Assertions.assertEquals; @SpringBootTest @Slf4j @Tag("first") public class FirstTest { @Test @Tag("easy") @Tag("important") @DisplayName("first-1") void first1Test() { log.info("first1Test"); assertEquals(2, Math.addExact(1, 1)); } @Test @Tag("easy") @DisplayName("first-2") void first2Test() { log.info("first2Test"); assertEquals(2, Math.addExact(1, 1)); } @Test @Tag("hard") @DisplayName("first-3") void first3Test() { log.info("first3Test"); assertEquals(2, Math.addExact(1, 1)); } }
package com.bolingcavalry.tag.service.impl; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import static org.junit.jupiter.api.Assertions.assertEquals; @SpringBootTest @Slf4j @Tag("second") public class SecondTest { @Test @Tag("easy") @DisplayName("second-1") void second1Test() { log.info("second1Test"); assertEquals(2, Math.addExact(1, 1)); } @Test @Tag("easy") @DisplayName("second-2") void second2Test() { log.info("second2Test"); assertEquals(2, Math.addExact(1, 1)); } @Test @Tag("hard") @Tag("important") @DisplayName("second-3") void second3Test() { log.info("second3Test"); assertEquals(2, Math.addExact(1, 1)); } }
2. 以下圖紅框,在彈出的窗口上新增一個JUnit配置:面試
3. 接下來的操做以下圖所示,Test kind選擇Tags,就會按照標籤過濾測試方法,Tag expression裏面填寫過濾規則,後面會詳細講解這個規則,這裏先填個已存在的標籤important:spring
4. 建立好JUnit配置後,執行下圖紅框中的操做便可執行單元測試:shell
mvn clean test -Dgroups="important"
4. 翻看日誌,可見只有打了important標籤的測試方法被執行了,以下圖紅框所示:數據庫
再去看看surefire插件給出的測試報告,報告文件在junitpractice\tag\target\surefire-reports目錄下,下圖紅框中的文件就是測試報告:
express
打開上圖紅框中的一個文件,以下圖紅框,可見只有打了important標籤的測試方法被執行了:api
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> <configuration> <!--要執行的標籤--> <groups>important</groups> <!--不要執行的標籤--> <excludedGroups>hard</excludedGroups> </configuration> </plugin> </plugins> </build>
操做符 | 做用 | 舉例 | 舉例說明 |
---|---|---|---|
& | 與 | important & easy | 既有important,又有easy標籤, 在本文是first1Test |
! | 非 | important & !easy | 有important,同時又沒有easy標籤, 在本文是second3Test |
| | 或 | important | hard | 有important標籤的,再加上有hard標籤的, 在本文是first1Test、first3Test、second3Test |
5. 再次執行這個配置,結果以下圖紅框所示,只有這三個方法被執行:first1Test、first3Test、second3Test,可見標籤表達式生效了:
6. 在maven命令和surefire插件中使用標籤表達式的操做就不在文中執行了,請您自行驗證;
@Test @Tag("hard") @DisplayName("first-3") void first3Test() { log.info("first3Test"); assertEquals(2, Math.addExact(1, 1)); }
package com.bolingcavalry.tag.service.impl; import org.junit.jupiter.api.Tag; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Tag("hard") public @interface Hard { }
@Test @Hard @DisplayName("first-3") void first3Test() { log.info("first3Test"); assertEquals(2, Math.addExact(1, 1)); }
package com.bolingcavalry.tag.service.impl; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Tag("hard") @Test public @interface HardTest { }
@HardTest @DisplayName("first-3") void first3Test() { log.info("first3Test"); assertEquals(2, Math.addExact(1, 1)); }
最後一塊兒來看看給標籤取名時有哪些要注意的地方:
2. 標籤名不能有這六個符號, ( ) & | !
微信搜索「程序員欣宸」,我是欣宸,期待與您一同暢遊Java世界...
https://github.com/zq2599/blog_demos