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

时间:2018-06-05 19:43:28

标签: java gradle junit junit4 junit5

我读了一篇关于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

1 个答案:

答案 0 :(得分:7)

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