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

dav*_*xxx 27 java junit4 maven junit5

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

我想用JUnit 5创建新的测试类,现在已经发布并提供了新的有趣功能.
怎么做 ?

dav*_*xxx 39

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

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

JUnit 5文档:

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

所以配置要简单得多.
请注意,junit-4api依赖项是可选的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>
Run Code Online (Sandbox Code Playgroud)

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


老路:下面是Maven Surefire 2.22.0

以下是与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>
Run Code Online (Sandbox Code Playgroud)

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

注1:在junit-vintage-engine(4.12.1)和junit(4.12)的依赖性不指定完全相同的版本.
这根本不是问题:

  • 他们的释放与他们之间没有关系

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

注2:maven-surefire-plugin与2.19.1版本无关,无论你想编译JUnit 5测试类还是JUnit 4和JUnit 5测试类.
下一版本的插件在JUnit 5测试执行期间确实会导致一些异常,但2.22.0最终解决了这个问题(参见答案的第一部分:" 更新:来自Maven Surefire 2.22.0 ").