【问题标题】:Executing JUnit 4 and JUnit 5 tests in a same build在同一个构建中执行 JUnit 4 和 JUnit 5 测试
【发布时间】:2019-11-03 08:06:49
【问题描述】:

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

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

【问题讨论】:

  • 我注意到一些流行的 BDD 工具尚未在 JUnit 5 下运行。
  • @Raedwald 当我写这篇文章时,我的问题之一是 Cucumber。对于支持多种语言的 BDD 工具,我注意到标准 Java 并不总是得到很好的更新/支持。

标签: java maven junit4 junit5


【解决方案1】:

JUnit 5 提供a way out of the box

JUnit 5 = JUnit 平台 + JUnit Jupiter + JUnit Vintage

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

JUnit Jupiter 是新的编程模型和扩展模型的结合,用于在 JUnit 5 中编写测试和扩展。

JUnit Vintage 提供了一个 TestEngine,用于在平台上运行基于 JUnit 3 和 JUnit 4 的测试。

JUnit 平台是在 JVM 上启动测试框架的基础


更新:来自 Maven Surefire 2.22.0

来自the JUnit 5 documentation

2.22.0 版本开始,Maven Surefire 提供原生支持 用于在 JUnit 平台上执行测试。

这样配置就简单多了。
请注意,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


旧方法:对于 Maven Surefire,低于 2.22.0

这是用于配置项目以编译和运行 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:无论您想编译 JUnit 5 测试类还是同时编译 JUnit 4 和 JUnit 5 测试类,使用 2.19.1 版本的 maven-surefire-plugin 都很重要。
插件的下一个版本在 JUnit 5 测试执行期间确实会导致一些异常,但 2.22.0 最终解决了问题(请参阅答案的第一部分:“更新:来自 Maven Surefire 2.22.0 ”)。

【讨论】:

  • 非常感谢马克!我测试了一个肯定为我添加了它的 Spring Boot 项目。我更新了。
  • 我注意到,如果您只有 JUnit 4 测试,Surfire 插件将无法识别其中任何一个。无论存在多少 JUnit 4 测试,您的项目都需要至少一个 JUnit Jupiter 测试来运行任何测试。
  • junit-vintage-engine 也应该在 test 范围内。
【解决方案2】:

JUnit 在https://github.com/junit-team/junit5-samples 有许多示例项目

我有一个 Gradle 项目,关注 https://github.com/junit-team/junit5-samples/tree/main/junit5-migration-gradle 为我工作。

https://github.com/junit-team/junit5-samples/tree/main/junit5-migration-maven 是 Maven 等价物 - 我没有尝试过,但我想它也可以。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 2018-02-05
    • 1970-01-01
    相关资源
    最近更新 更多