学习如何使用 Maven 配置 JUnit 6。本教程讨论了我们需要包含的工件,如何设置 pom.xml,以及如何执行测试。由于 JUnit 6 构建在 JUnit Platform 之上,因此我们必须确保类路径上存在一个TestEngine。
请注意,JUnit 6 需要 Java 17 的最低版本和 Maven Surefire / Failsafe 插件 3.0.0(或更高版本)才能运行测试。
1. JUnit 6 模块
下表总结了 JUnit 6 中包含的模块及其用途
| Maven 工件 | 描述 | 必需? |
|---|---|---|
| junit-jupiter | 方便的“启动器”工件,它同时引入了 junit-jupiter-api 和 junit-jupiter-engine。这是使用 Maven 使用 JUnit 6 的最简单方法。 | 是(推荐) |
| junit-jupiter-api | 包含所有公共注释和 API 类,例如 @Test、@BeforeEach、Assertions 等。需要编译测试。 | 如果使用 junit-jupiter → 否;否则 是 |
| junit-jupiter-engine | 实际运行 JUnit 6 测试的 TestEngine 实现。必须在测试运行时存在。 | 如果使用 junit-jupiter → 否;否则 是 |
| junit-platform-launcher | 用于以编程方式启动 JUnit Platform。适用于自定义测试运行器或 IDE。 | 否 |
| junit-platform-suite-api | 提供诸如 @Suite 之类的注释,以分组和执行测试套件。 | 否 |
| junit-platform-reporting | JUnit Platform 运行的附加报告功能。 | 否 |
| junit-vintage-engine | 允许在 JUnit Platform 上运行遗留的 JUnit 4 和 JUnit 3 测试。 | 可选,仅用于旧测试支持 |
| junit-bom | BOM(物料清单)用于自动对齐所有 JUnit 模块版本。 | 推荐但不是必需的 |
2. 最小 JUnit 6 Maven 示例
下面给出了一个示例 pom.xml 文件。
<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.howtodoinjava</groupId>
<artifactId>examples</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>examples</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>17</maven.compiler.release>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>6.0.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.4</version>
</plugin>
</plugins>
</build>
</project>
3. 生产就绪的 JUnit 6 设置示例
此配置具有以下测试工具支持
| 功能 | 描述 |
|---|---|
| BOM 导入 | 自动对齐所有 JUnit 6 模块版本 |
| Jupiter Engine + Params | 完全支持现代 JUnit 6 功能 |
| Suite API | 支持使用 @Suite 注解的测试套件 |
| Launcher API | 允许以编程方式运行测试(IDE、CI 工具) |
| Surefire + Failsafe | 明确的分离:单元测试 + 集成测试 |
| 并行执行 | 通过 junit-platform.properties 启用 |
| Jacoco 已就绪 | 自动代码覆盖率报告 |
| 自定义测试包含 | 更简洁的文件命名约定 |
<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>junit6-advanced-demo</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.platform.properties>junit-platform.properties</junit.platform.properties>
</properties>
<!-- Import JUnit BOM for consistent versions -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>6.0.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- JUnit 6 starter: API + Engine -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<!-- Parameterized tests -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<!-- Suite API for @Suite test classes -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-api</artifactId>
<scope>test</scope>
</dependency>
<!-- Programmatic launching, used by some tools -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
<!-- Optional: AssertJ for fluent assertions -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.26.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Surefire for Unit Tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<useModulePath>false</useModulePath>
<includes>
<include>**/*Test.java</include>
<include>**/*Tests.java</include>
<include>**/*TestCase.java</include>
</includes>
<!-- Enable JUnit Platform -->
<properties>
<property>
<name>junit.platform.properties</name>
<value>${junit.platform.properties}</value>
</property>
</properties>
</configuration>
</plugin>
<!-- Failsafe for integration tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.2.5</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/*IT.java</include>
</includes>
</configuration>
</plugin>
<!-- Optional: JaCoCo test coverage -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.12</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
4. 结论
通过其模块化架构和统一的 JUnit Platform,使用 Maven 设置 JUnit 6 变得简单明了。通过包含强制性的 junit-jupiter-engine 以及可选模块(例如 API、params 和 platform 组件),我们可以组装项目所需的测试堆栈。
祝您学习愉快!!
评论