首页 文章

如何在相同的Gradle构建中运行JUnit5和JUnit4?

提问于
浏览
5

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

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

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

如何使我的构建能够一起运行JUnit4和JUnit5 . 谢谢 .

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

1 回答

  • 6

    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")
    }
    

相关问题