java - 在同一个构建中执行 JUnit 4 和 JUnit 5 测试

标签 java maven junit4 junit5

在 Maven 项目中,我有一些现有的测试依赖于 JUnit 4。由于多种原因,我无法在 JUnit 5 中迁移这些测试。
本质上,一些测试依赖于使用 JUnit 4 运行器的库,代码迁移可能需要一些时间。

我同样希望使用现已发布并提供新的有趣功能的 JUnit 5 创建新的测试类。
该怎么做?

最佳答案

JUnit 5 提供 a way out of the box .

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

每一个都是一个不同的项目,使用它们可以在同一个项目中编译和执行 JUnit 4 和 JUnit 5 测试。

JUnit Jupiter is the combination of the new programming model and extension model for writing tests and extensions in JUnit 5.

JUnit Vintage provides a TestEngine for running JUnit 3 and JUnit 4 based tests on the platform.

The JUnit Platform serves as a foundation for launching testing frameworks on the JVM


更新:来自 Maven Surefire 2.22.0

来自 the JUnit 5 documentation :

Starting with version 2.22.0, Maven Surefire provides native support for executing tests on the JUnit Platform.

这样配置就简单多了。
请注意,junit-4 api 依赖项是可选的,因为现在需要的 engine 依赖项已经拉取了默认的 api 版本(就是这种情况对于junit 4和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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>david</groupId>
    <artifactId>jupiter-4-and-5-same-build</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <junit-jupiter.version>5.1.0</junit-jupiter.version>
        <!-- optional : if we want to use a junit4 specific version -->
        <junit.version>4.12</junit.version>
    </properties>
    <dependencies>
        <!--JUnit Jupiter Engine to depend on the JUnit5 engine and JUnit 5 API -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <!--JUnit Jupiter Engine to depend on the JUnit4 engine and JUnit 4 API  -->
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>${junit-jupiter.version}</version>
        </dependency>
        <!-- Optional : override the JUnit 4 API version provided by junit-vintage-engine -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>
        </plugins>
    </build>

</project>

在我的 GitHub 空间上,我添加了一个可以浏览/克隆的工作示例 maven 项目。 网址:https://github.com/ebundy/junit4-and-5-minimal-maven-project


旧方法:适用于 2.22.0 下的 Maven Surefire

这是用于配置项目以编译和运行 JUnit4 和 JUnit5 测试的最小配置:

<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>mygroup</groupId>
    <artifactId>minimal-conf-junit4-5</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <!-- JUnit 5 depends on JDK 1.8 -->
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <!--  JUnit dependency versions -->
        <junit.version>4.12</junit.version>
        <junit-vintage-engine>4.12.1</junit-vintage-engine>
        <junit-jupiter.version>5.0.1</junit-jupiter.version>
        <junit-platform.version>1.0.1</junit-platform.version>
    </properties>

    <dependencies>
        <!--JUnit Jupiter API to write and compile tests with JUnit5 -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <!-- JUnit 4 to make legacy JUnit 4 tests compile -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version> <!-- matters until now-->
                <dependencies>
                    <!-- to let surefire to run JUnit 4 but also JUnit 5 tests -->
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>${junit-platform.version}</version>
                    </dependency>
                    <!-- JUnit vintage engine to run JUnit 3 or JUnit 4 tests -->
                    <dependency>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                        <version>${junit-vintage-engine}</version>
                    </dependency>
                    <!-- JUnit 5 engine to run JUnit 5 tests -->
                    <dependency>
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>${junit-jupiter.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

现在 mvn test 编译并运行 JUnit 4 和 JUnit 5 测试。

注 1:junit-vintage-engine (4.12.1) 和 junit (4.12 ) 依赖项没有指定相同的确切版本。
这根本不是问题:

  • 他们之间的发布不相关

  • junit-vintage-engine 旨在运行任何 JUnit 34 测试。

注意 2:带有 2.19.1 版本的 maven-surefire-plugin 对编译 JUnit 5 测试类或 JUnit 4 和 JUnit 5 测试类都很重要。
插件的下一个版本在 JUnit 5 测试执行期间确实会导致一些异常,但 2.22.0 最终解决了问题(请参阅答案的第一部分:“更新:来自 Maven Surefire 2.22.0”)。

关于java - 在同一个构建中执行 JUnit 4 和 JUnit 5 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47158583/

相关文章:

java - 我的语言翻译在谷歌浏览器和 IE 中不起作用

java - 在 @Rule 运行之前,如何使用 Guice 在 Junit 测试用例中注入(inject)依赖项?

java - "Unreleased Resource: Database"确认问题

java - 我无法从 derby 包导入 JDBC 客户端驱动程序

java - 为什么我的项目导入jdk 1.8,而pom.xml设置jdk 1.7?

java - 如何禁止访问依赖项的依赖项

UI线程上的Android Junit4测试

java - 使用 selenium.open() 打开带参数的 URL

java - 有没有办法在程序完成后循环它?我总是遇到无限循环

java - 如何使用 Facebook Android SDK 4.x 将视频上传到 Facebook?