【问题标题】:How to run JUnit5 and JUnit4 in same Gradle build?如何在同一个 Gradle 构建中运行 JUnit5 和 JUnit4?
【发布时间】:2018-06-05 19:43:28
【问题描述】:

我阅读了有关 Maven 的答案,但我想知道如何在 Gradle 中完成这项任务 - Executing JUnit 4 and JUnit 5 tests in a same build

目前我的 Gradle 构建只接受以下测试: import org.junit.jupiter.api.Test;

我的问题是我正在使用@RunWith,它需要 JUnit4 才能运行,但我想在 JUnit5 Vintage Engine 上执行它。

如何进行构建以便可以同时运行 JUnit4 和 JUnit5。谢谢。

更新: 现在有一个用于 JUnit 5 的原生 Mockito Junit Jupiter - https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter

【问题讨论】:

    标签: java gradle junit junit4 junit5


    【解决方案1】:

    junit5-migration-gradle 项目演示了如何使用 Gradle 基于 JUnit 5 执行测试。此外,它还展示了现有的基于 JUnit 4 的测试可以在与基于 JUnit Jupiter 的测试或 JUnit 平台支持的任何其他测试相同的测试套件中执行。

    基本上归结为在运行时类路径上同时拥有 Jupiter 和 Vintage 这两个引擎:

    dependencies {
        testCompile("org.junit.jupiter:junit-jupiter-api:5.2.0")
        testRuntime("org.junit.jupiter:junit-jupiter-engine:5.2.0")
    }
    
    dependencies {
        testCompile("junit:junit:4.12")
        testRuntime("org.junit.vintage:junit-vintage-engine:5.2.0")
    }
    

    【讨论】:

    • 我相信应该不止这些 - 我的 JUnit4 测试仍未在 ./gradlew clean test 上执行
    • 您能否发布更多信息,例如您的build.gradle 文件或提供指向显示问题的最小示例项目的链接?
    • 您还需要将此子句添加到您的 build.gradle: test { useJUnitPlatform() } 或者,如果您使用 Kotlin 并且有一个 build.gradle.kts: tasks.test { useJUnitPlatform() }
    猜你喜欢
    • 1970-01-01
    • 2019-07-14
    • 2020-07-21
    • 1970-01-01
    • 2017-09-16
    • 2014-02-08
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多