JUnit 6 Maven Dependency

学习如何配置 JUnit 6 Maven 依赖项,包括强制的 junit-jupiter-engine 以及可选组件,例如 API、params 和 platform 组件。

JUnit6 Logo

学习如何使用 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-apijunit-jupiter-engine。这是使用 Maven 使用 JUnit 6 的最简单方法。是(推荐)
junit-jupiter-api包含所有公共注释和 API 类,例如 @Test@BeforeEachAssertions 等。需要编译测试。如果使用 junit-jupiter;否则
junit-jupiter-engine实际运行 JUnit 6 测试的 TestEngine 实现。必须在测试运行时存在。如果使用 junit-jupiter;否则
junit-platform-launcher用于以编程方式启动 JUnit Platform。适用于自定义测试运行器或 IDE。
junit-platform-suite-api提供诸如 @Suite 之类的注释,以分组和执行测试套件。
junit-platform-reportingJUnit Platform 运行的附加报告功能。
junit-vintage-engine允许在 JUnit Platform 上运行遗留的 JUnit 4 和 JUnit 3 测试。可选,仅用于旧测试支持
junit-bomBOM(物料清单)用于自动对齐所有 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 组件),我们可以组装项目所需的测试堆栈。

祝您学习愉快!!

评论

订阅
通知
0 条评论
最多投票
最新 最旧
内联反馈
查看所有评论

关于我们

HowToDoInJava 提供 Java 和相关技术的教程和操作指南。

它还分享最佳实践、算法和解决方案以及经常被问到的面试题。

我们的博客

REST API 教程

关注我们