JUnit 5 Maven 依赖

位置:首页>文章>详情   分类: Java教程 > 编程技术   阅读(355)   2023-06-26 07:54:18

学习使用 Maven 配置 JUnit 5、不同的 JUnit 模块以及如何使用这些模块来创建和执行测试。

请注意,JUnit 5 在运行时至少需要 Java 8

1. JUnit 5 Maven 依赖

要通过 Maven 运行 JUnit 5 测试,以下是主要的必需依赖项:

下面给出了一个示例 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 
        https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cundage</groupId>
    <artifactId>JUnit5Examples</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
        <junit.jupiter.version>5.9.1</junit.jupiter.version>
        <junit.platform.version>1.9.1</junit.platform.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite</artifactId>
            <version>${junit.platform.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <argLine>
                        --illegal-access=permit
                    </argLine>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <argLine>
                        --illegal-access=permit
                    </argLine>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>  

2. 使用 JUnit 5 执行 JUnit 4 测试

要在 JUnit 5 环境中执行 JUnit 4 测试,您需要 JUnit Platform Surefire Provider 插件。

它可以运行基于JUnit 4 的测试,只要你配置一个junit 依赖并将JUnit Vintage 测试引擎实现添加到maven-surefire-plugin 的依赖中,类似如下。

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.0.0-M4</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                    <version>4.12.0-M4</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>

通过在 pom.xml 中配置以上内容,您现在可以使用 JUnit 5 运行旧测试。

快乐学习!!

标签2: JUnit 5 Maven Basics
地址:https://www.cundage.com/article/junit5-maven-dependency.html

相关阅读

学习使用 Maven 配置 JUnit 5、不同的 JUnit 模块以及如何使用这些模块来创建和执行测试。 请注意,JUnit 5 在运行时至少需要 Java 8。 1. JUnit 5 Mav...
Maven 是项目依赖和构建管理的绝佳工具。它可用于运行项目的 Junit 测试用例。在这篇文章中,我将展示一些简单但有用的命令示例,以各种方式运行测试用例。 为了演示,我使用以下命令创建了一个...
学习为 JUnit 测试的执行结果创建 HTML 报告。在这个例子中,我正在为 JUnit-Examples 项目创建一个 HTML 报告Github。 1. Maven Surefire 报告...
JUnit 5 测试套件 是用@Suite 注释编写的。套件帮助我们运行分布到多个类和包中的测试。 我们可以使用 Include 和 Exclude 注释(在本教程后面讨论)来过滤测试包、测试类...
在这个 JUnit 5 教程中,我们将学习如何在新支持的 创建 XML 报告 -team/open-test-reporting"&gtl;打开测试报告格式并以旧格式生成它们以实现向后兼容性。 ...
学习使用 Gradle 配置 JUnit 5、它的不同模块以及如何使用它们来创建和执行测试。 请注意,JUnit 5 在运行时需要 Java 8。 1. JUnit 5 Gradle 依赖 要通...
Eclipse 为 JUnit 测试用例提供了出色的工具支持。在 Eclipse 中为 JUnit 测试用例配置代码模板 是加快测试开发的完美补充。 学习在 Eclipse 中创建和导入 JUn...
JUnit 5 @ BeforeAll 注解表示一个方法,它是一个生命周期方法。 @BeforeAll 是 JUnit 4 中 @BeforeClass 注解的替代品。 1. @BeforeAl...
@AfterEach 注释用于表示被注释的方法应该在每个 @Test @RepeatedTest、@ParameterizedTest 或@TestFactory 当前类中的方法。 JUnit ...
在 JUnit 5 中,@BeforeEach 注释用于指示在每次调用 @Test、@RepeatedTest&lt; 之前应执行注释的方法/em&gtl;、@ParameterizedTest...