Gradle项目在IntelliJ中运行jUnit 5测试

我正在尝试Gradle和jUnit5。 除了我不能运行特定的jUnit测试之外,一切正常。 当我右键单击测试类时,不会出现“运行’SampleTest’”选项。

我有最新版本的IntelliJ(2016.1.3)Ultimate。 这是我的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

IntelliJ 2016.1.3不支持JUnit 5测试。 但是,您可以添加注释@RunWith(JUnitPlatform.class) ,它将以JUnit 4兼容模式执行测试(您仍然可以使用所有JUnit 5function)。 有关更多信息,请参见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

最新的Idea 2016.2现在支持JUnit 5框架。 你可以不用junit-gradle-plugin直接运行JUnit5测试。 请参阅INTELLIJ IDEA的新内容 。 将Idea升级到此新版本后,您可以创建一个gradle项目并执行以下步骤来测试如何运行JUnit 5测试。

  • 的build.gradle

     apply plugin: 'java' compileTestJava { sourceCompatibility = 1.8 targetCompatibility = 1.8 } repositories { mavenCentral() } dependencies { testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M1") testRuntime("org.junit.vintage:junit-vintage-engine:4.12.0-M1") //NOTE: if you replaced above testRuntime dependency with following //testRuntime("org.junit.jupiter:junit-jupiter-engine:5.0.0-M1") //this test would fail. } 
  • 在测试源文件夹中创建一个FirstJUnit5Test类

     import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; public class FirstJUnit5Test { @Test void myFirstTest() { assertEquals(2, 1 + 1); } } 
  • 右键单击左侧项目窗格中的此测试类,然后选择“运行’FirstJUnit5Test’。您将看到如下结果: 在此处输入图像描述
  • 有关更多信息,您可以从github签出此项目 。

UPDATE

对于IDEA 2016.3.3及更高版本, dependencies配置可简化为:

 dependencies { testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M3") } 

您必须使用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 ,然后做以下两件事。

再添加一个依赖项

   org.junit.platform junit-platform-runner ${junit.platform.version}  

然后将您的测试类公开并使用@RunWith(JUnitPlatform.class)对其进行注释。

您的IDE现在将该类识别为测试并将提供集成。