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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47158583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:30:48  来源:igfitidea点击:

Executing JUnit 4 and JUnit 5 tests in a same build

javamavenjunit4junit5

提问by davidxxx

In Maven projects, I have some existing tests relying on JUnit 4. I cannot migrate these tests in JUnit 5 for multiple reasons.
Essentially, some tests depend on a library which uses JUnit 4 runner and code migration may take time.

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

I would like all the same create new test classes with JUnit 5 that is now released and provides new interesting features.
How to do that ?

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

回答by davidxxx

JUnit 5 provides a way out of the box.

JUnit 5 提供了一种开箱即用的方法

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

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

Each one is a distinct project and using all of them allows to compile and execute JUnit 4 and JUnit 5 tests in a same project.

每一个都是一个不同的项目,使用所有这些允许在同一个项目中编译和执行 JUnit 4 和 JUnit 5 测试。

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

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

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

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

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

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



Update : from Maven Surefire 2.22.0

更新:来自 Maven Surefire 2.22.0

From the JUnit 5 documentation:

JUnit 5 文档

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

从 version 开始2.22.0,Maven Surefire 为在 JUnit 平台上执行测试提供本机支持。

So the configuration is much simpler.
Note that the junit-4api dependency is optional as the enginedependencies that are now required already pull a default apiversion (it is the case for both junit 4 and 5).

所以配置要简单得多。
请注意,junit-4api 依赖项是可选的engine,因为现在需要的依赖项已经拉取了默认api版本(junit 4 和 5 都是这种情况)。

Here is a sample pom.xml.

这是一个示例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>

On my GitHub space I added a working sample maven project that you can browse/clone. URL: https://github.com/ebundy/junit4-and-5-minimal-maven-project

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



Old way : for Maven Surefire below 2.22.0

旧方法:对于下面的 Maven Surefire 2.22.0

Here is the minimal configuration to use with Maven to configure the project to compile and run both JUnit4 and JUnit5 tests :

这是与 Maven 一起使用的最小配置,用于配置项目以编译和运行 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>

Now mvn testcompiles and runs both JUnit 4 and JUnit 5 tests.

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

Note 1 : the junit-vintage-engine(4.12.1) and the junit(4.12) dependencies don't specify the same exact version.
This is not an issue at all as :

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

  • their release are not related between them

  • junit-vintage-engineis designed to run any JUnit 3or 4tests.

  • 他们的释放与他们无关

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

Note 2 : maven-surefire-plugin with the 2.19.1version matters whatever you want to compile JUnit 5 test classes or both JUnit 4 and JUnit 5 test classes.
Next version of the plugin causes indeed some exceptions during JUnit 5 tests execution but the 2.22.0that at last solves the issue (see the first part of the answer : "Update : from Maven Surefire 2.22.0").

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