概述
寫測驗用例對于開發來說有2點好處,一是開發階段寫完的功能可以快速驗證,第二就是在后期需求變動或修改BUG后可以快速測驗當前改動是否帶來其它問題,下面就了解一下Junit5寫測驗用例,
準備
創建一個maven專案
mkdir junit5-tutorial
cd junit5-tutorial
mkdir -p src/main/java
mkdir -p src/test/java
mkdir -p src/main/resources
mkdir -p src/test/resources
# 撰寫pom.xml
vi pom.xml
添加依賴
- 引入第三方斷言庫assertj
- 支持json測驗
- 支持xml測驗
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.xxx</groupId>
<artifactId>junit5-tutorial</artifactId>
<version>1.0-SNAPSHOT</version>
<name>junit5-tutorial</name>
<url>https://www.xxx.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<build>
<pluginManagement>
<plugins>
<!-- 在這里宣告的目的是使用指定的版本 -->
<!-- 執行測驗用例任務的插件,默認系結test生命周期的test階段 -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M6</version>
</plugin>
<!-- 用來執行編譯任務的插件,默認系結default生命周期compile階段 -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencyManagement>
<dependencies>
<!-- XML Unit - Dependency Management -->
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.12.10</version>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>1.12.10</version>
<scope>test</scope>
</dependency>
<!-- Mockito Dependency -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.5.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.22.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>4.8.146</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
</dependency>
<!-- JSON Unit - Dependencies -->
<dependency>
<groupId>net.javacrumbs.json-unit</groupId>
<artifactId>json-unit-assertj</artifactId>
<version>2.33.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.2.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.13.2</version>
</dependency>
<!-- XML Unit - Dependencies -->
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-assertj</artifactId>
<version>2.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
創建一個User
public record User(String name, Integer age, Boolean blocked, LocalDate birthDate) {
}
測驗
測驗用例命名最佳實踐
首先測驗類名應該以Test結尾,測驗用例名稱最好遵從以下規則
- 測驗名稱應表達特定要求
- 測驗名稱應包含預期的輸入或預期的結果
- 測驗名稱應以陳述的形式
具體參考:https://osherove.com/blog/2005/4/3/naming-standards-for-unit-tests.html
斷言
@Test
@DisplayName("User should be at least 18")
void user_should_be_at_least_18() {
// junit5 的斷言
assertTrue(user.age() >= 18);
// assertj 的斷言
assertThat(user.age()).isGreaterThanOrEqualTo(18);
}
顯示名稱
測驗類和測驗方法可以宣告自定義顯示名稱,可以使用空格、特殊字符、甚至emojis表情符號,這些名稱會在runner和測驗報告中顯示,

引數化測驗
引數化測驗可以用不同的引數多次運行測驗,它們和普通的@Test方法一樣宣告,但是使用@ParameterizedTest注解,還必須宣告至少一個將為每次呼叫提供引數的來
使用@ValueSource來指定引數來源
它可以指定一個原生型別的陣列,并且只能為每次呼叫提供一個引數
@ParameterizedTest
@ValueSource(ints = {20, 50, 80})
void test_value_source(int age) {
assertThat(age).isGreaterThanOrEqualTo(18);
}
讀取CSV檔案內容作為引數來源
它可以讓你使用classpath中的csv檔案,csv檔案中的每一行都會導致引數測驗的一次呼叫
src/test/resources/friends.csv
name,age
lisa,20
hans,30
hanna,40
@ParameterizedTest
@CsvFileSource(resources = "/friends.csv", numLinesToSkip = 1)
void test_value_source_by_csv_file_source(String name, int age) {
assertThat(age).isGreaterThanOrEqualTo(18);
}
標簽
我們可以給測驗類或測驗用例上面通過@Tag加標簽,執行測驗的時候可以指定標簽,從而達到為測驗用例分組的目的,
下面就給測驗類打上一個integration的標簽
@Tag("integration")
class User01Test {
// ...
}
可以使用如下命令來指定要執行的測驗用例:
mvn test -Dgroups="integration"

左側只執行了Running com.example.xxx.User01Test一個測驗類,右側則執行了3個
總結
本文介紹了如何使用Junit5寫測驗用例,
參考
- Unit test naming best practices Unit test naming best practices - Stack Overflow
- https://osherove.com/blog/2005/4/3/naming-standards-for-unit-tests.html
- AssertJ Homepage https://assertj.github.io/doc/
- Gradle: https://stackoverflow.com/a/64986861
- https://junit.org/junit5/docs/current/user-guide/#running-tests-tag-expressions
- further reading
- JUnit 5 User Guide
- JUnit 5 中文檔案 https://doczhcn.gitbook.io/junit5/
- AssertJ - fluent assertions java library
- Jupiter / JUnit 5 - Testcontainers
- awaitility/awaitility: Awaitility is a small Java DSL for synchronizing asynchronous operations (github.com)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/499024.html
標籤:Java
上一篇:Java Long類toBinaryString()方法功能簡介說明
下一篇:Java面向物件(五)
