问题
I want to convert my Gradle project test from JUnit 4 to JUnit 5. As there are a lot of tests, I don't want to convert them all at the same time.
I try to configure my build.gradle
like this:
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'
}
Old test are still running, but Intellij didn't recognize the new JUnit 5 test like this one:
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class JUnit5Test {
@Test
void test() {
assertTrue(true);
}
}
I'm using Intellij 2016.2 with gradle 2.9
回答1:
Since version 4.6 for Gradle, there is no need for plugins anymore
Gradle supports Junit5 natively just do:
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'
}
}
回答2:
Currently Intellij IDEA supports JUnit5.
Take a look at nice article about integrating JUnit5 with IDEA: Using JUnit 5 in IntelliJ IDEA
来源:https://stackoverflow.com/questions/38576011/upgrade-from-junit-4-to-junit-5-in-intellij-with-gradle