我现在正在尝试Gradle和jUnit5。除了我不能运行特定的jUnit测试之外,一切都很好。当我右键单击一个测试类时,"Run‘SampleTest“选项不会出现。
我有最新版本的IntelliJ (2016.1.3)终极版。这是我的build.gradle文件:
repositories {
mavenCentral()
}
apply plugin: 'java'
version = '1.0.0-SNAPSHOT'
jar {
baseName = 'test-project'
}
dependencies {
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.0-M1'
}项目结构是标准的(如Maven中的)。下面是一个测试的例子:
package com.test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class SampleTest {
@Test public void sampleTest() {
int test = 1;
Assertions.assertTrue(test == 1);
}
}我遗漏了什么?
编辑:
看来Gradle也没有拿起我的测试。当我进入build/reports/tests/index.html时,它表示0测试。
最终编辑:
下面是@dunny的回答,下面是我为使一切正常运转所做的一切。我修改了我的build.gradle文件如下:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M1'
}
}
repositories {
mavenCentral()
}
apply plugin: 'java'
apply plugin: 'org.junit.platform.gradle.plugin'
version = '1.0.0-SNAPSHOT'
jar {
baseName = 'test-project'
}
dependencies {
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.0-M1'
testCompile group: 'org.junit.platform', name: 'junit-platform-runner', version: '1.0.0-M1'
testCompile group: 'junit', name: 'junit', version: '4.12'
testRuntime group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.0-M1'
}
test {
testLogging {
events 'started', 'passed'
}
}在IntelliJ中,我打开了Gradle窗口,并单击“刷新所有gradle项目”按钮,以刷新库。
然后,在我的测试类中,我在类声明的顶部添加了@RunWith(JUnitPlatform.class)。
当我做gradle build时,结果可以在这里获得:build\test-results\junit-platform\TEST-junit-jupiter.xml
发布于 2016-07-10 16:41:30
IntelliJ 2016.1.3不支持JUnit 5测试。但是,您可以添加注释@RunWith(JUnitPlatform.class),它将在JUnit 4兼容模式下执行测试(仍然可以使用JUnit 5的所有特性)。有关详细信息,请参阅http://junit.org/junit5/docs/current/user-guide/#running-tests-junit-platform-runner。
对于Gradle,您需要包含Gradle JUnit 5插件以启用支持:
buildscript {
repositories {
mavenCentral()
// The following is only necessary if you want to use SNAPSHOT releases.
// maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}
dependencies {
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M1'
}
}
apply plugin: 'org.junit.platform.gradle.plugin'请参阅http://junit.org/junit5/docs/current/user-guide/#running-tests-build
发布于 2016-07-21 16:35:44
最新的Idea 2016.2现在支持JUnit 5框架。您可以直接运行JUnit5测试,而不需要junit-gradle-plugin。请看INTELLIJ的新理念是什么?。在将您的想法升级到这个新版本之后,您可以创建一个gradle项目并执行以下步骤来测试如何运行JUnit 5测试。

更新
对于IDEA 2016.3.3及更高版本,可以将dependencies配置简化为:
dependencies {
testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M3")
}发布于 2016-07-14 09:49:05
您必须使用使用JUnit 4运行程序运行测试,因为IntelliJ 2016.1.3没有JUnit 5测试运行程序。
如果从文档中链接的示例pom.xml ( https://github.com/junit-team/junit5-samples/blob/r5.0.0-M1/junit5-maven-consumer/pom.xml )开始,则执行以下两项操作。
再添加一个依赖项
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
</dependency>然后,使您的测试类成为公共,并使用@RunWith(JUnitPlatform.class)对其进行注释。
您的IDE现在将将该类识别为测试,并将提供集成。
https://stackoverflow.com/questions/38293901
复制相似问题