在同一版本中执行JUnit 4和JUnit 5测试 [英] Executing JUnit 4 and JUnit 5 tests in a same build

查看:162
本文介绍了在同一版本中执行JUnit 4和JUnit 5测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我希望所有相同的东西都可以使用现在发布的JUnit 5创建新的测试类,并提供有趣的新功能.
该怎么做?

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提供了

JUnit 5 provides a way out of the box.

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

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

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

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 Jupiter 是新的编程模型和扩展模型的组合,用于在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 提供了一个TestEngine,用于在平台上运行基于JUnit 3和JUnit 4的测试.

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

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

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


更新:从Maven Surefire 2.22.0


Update : from Maven Surefire 2.22.0

来自 JUnit 5文档 :

从版本2.22.0开始,Maven Surefire提供本机支持 用于在JUnit平台上执行测试.

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都是这种情况).

So the configuration is much simpler.
Note that the junit-4 api dependency is optional as the engine dependencies that are now required already pull a default api version (it is the case for both junit 4 and 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

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

旧方法:对于2.22.0

Old way : for Maven Surefire below 2.22.0

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

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

<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测试.

Now mvn test compiles and runs both JUnit 4 and JUnit 5 tests.

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

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 :

  • 它们的版本之间没有关系

  • their release are not related between them

junit-vintage-engine设计为运行任何JUnit 3 4 测试.

junit-vintage-engine is designed to run any JUnit 3 or 4 tests.

注2:具有2.19.1版本的maven-surefire-plugin与要编译JUnit 5测试类或同时编译JUnit 4和JUnit 5测试类无关. 下一版本的插件确实会在JUnit 5测试执行期间引起某些异常,但最终解决了该问题的2.22.0(请参阅答案的第一部分:" Update:来自Maven Surefire 2.22.0 " ").

Note 2 : maven-surefire-plugin with the 2.19.1 version 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.0 that at last solves the issue (see the first part of the answer : "Update : from Maven Surefire 2.22.0").

这篇关于在同一版本中执行JUnit 4和JUnit 5测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆