本文介绍了在intellij中用Gradle从JUnit 4升级到JUnit 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将我的Gradle项目测试从JUnit 4转换为JUnit 5.由于有很多测试,我不想同时将它们全部转换。
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.
我尝试像这样配置我的 build.gradle
:
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'
}
旧测试仍在运行,但Intellij没有认出像这样的新的JUnit 5测试:
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);
}
}
我使用Intellij 2016.2和Gradle 2.9 p>
I'm using Intellij 2016.2 with gradle 2.9
推荐答案
从Gradle版本4.6开始,不再需要插件了
Since version 4.6 for Gradle, there is no need for plugins anymore
Gradle本地支持Junit5:
Gradle supports Junit5 natively just do:
dependencies {
test.useJUnitPlatform()
testImplementation "org.junit.jupiter:junit-jupiter-params:$junitVersion"
testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
testRuntimeOnly "org.junit.vintage:junit-vintage-engine:4.12.0"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
}
这篇关于在intellij中用Gradle从JUnit 4升级到JUnit 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!