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

问题描述 投票:0回答:4

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

我仍然希望使用现已发布的 JUnit 5 创建新的测试类,并提供新的有趣功能。
如何做到这一点?

java maven junit junit4 junit5
4个回答
83
投票

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-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

以下是使用 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>

现在

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 测试执行期间导致一些异常,但最终解决了问题(请参阅答案的第一部分:“
更新:来自 Maven Surefire 2.22.0
”)。


1
投票
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 的等价物 - 我还没有尝试过,但我想它也可以工作。


0
投票

2.22.0

还添加测试以使用 junitPlatform

dependencies { testImplementation 'junit:junit:4.13.1' testImplementation 'org.junit.vintage:junit-vintage-engine' }

然后 junit4 和 junit5 测试应该可以正常工作。


0
投票
kts

): test { useJUnitPlatform() }

tasks.withType<Test> {
    useJUnitPlatform() // Enables JUnit Platform (JUnit 5 + JUnit 4)
}

dependencies {
    // Aggregator dependency that also brings JUnit 5 parameterized tests etc.
    testImplementation("org.junit.jupiter:junit-jupiter:5.10.0")
    // The Vintage engine is needed to be able to run JUnit 4 tests
    testImplementation("org.junit.vintage:junit-vintage-engine:5.10.0")
}
通过运行 Gradle 
public class MyTestClass { @org.junit.jupiter.api.Test // <- JUnit 5 test public void jUnit5test() { /* ... */ } @org.junit.Test // <- JUnit 4 test public void jUnit4Test() { /* ... */ } }

test
任务来执行测试。
    

© www.soinside.com 2019 - 2024. All rights reserved.