使用gradle从intellij的JUnit 4升级到JUnit 5

Upgrade from JUnit 4 to JUnit 5 in intellij with gradle

我想将Gradle项目测试从JUnit 4转换为JUnit5。因为有很多测试,所以我不想同时转换它们。

我尝试这样配置build.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
apply plugin: 'java'

compileTestJava {
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
}

repositories {
    mavenCentral()
}

dependencies {
    testCompile("junit:junit:4.12")
    testCompile 'org.junit.jupiter:junit-jupiter-api:5.0.0-M2'
    testRuntime("org.junit.vintage:junit-vintage-engine:4.12.0-M2")
    testRuntime 'org.junit.jupiter:junit-jupiter-engine:5.0.0-M2'
}

旧的测试仍在运行,但是Intellij无法识别新的JUnit 5测试,如下所示:

1
2
3
4
5
6
7
8
9
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class JUnit5Test {
    @Test
    void test() {
        assertTrue(true);
    }
}

我正在使用gradle 2.9的Intellij 2016.2


从Gradle的4.6版开始,不再需要插件

Gradle本身就支持Junit5:

1
2
3
4
5
6
7
8
9
10
11
12
13
dependencies {      
    testImplementation"org.junit.jupiter:junit-jupiter-params:$junitVersion"
    testImplementation"org.junit.jupiter:junit-jupiter-api:$junitVersion"

    testRuntimeOnly"org.junit.vintage:junit-vintage-engine:$junitVersion"
    testRuntimeOnly"org.junit.jupiter:junit-jupiter-engine:$junitVersion"
}

test {
    useJUnitPlatform {
        includeEngines 'junit-jupiter', 'junit-vintage'
    }
}


当前,Intellij IDEA支持JUnit5。

看一下有关将JUnit5与IDEA集成的不错的文章:
在IntelliJ IDEA中使用JUnit 5