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

Jea*_*hef 12 java junit intellij-idea gradle junit5

我正在尝试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'
}
Run Code Online (Sandbox Code Playgroud)

项目结构是标准结构(如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);
  }
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

编辑:

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'
    }
}
Run Code Online (Sandbox Code Playgroud)

在IntelliJ中,我打开了Gradle窗口,然后单击"刷新所有gradle项目"按钮,刷新库.

然后在我的测试类中,我添加@RunWith(JUnitPlatform.class)了类声明.

当我这样做时gradle build,结果可以在这里找到:build\test-results\junit-platform\TEST-junit-jupiter.xml

wal*_*lsh 13

最新的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.
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 在测试源文件夹中创建一个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);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 右键单击左侧项目窗格中的此测试类,然后选择"运行'FirstJUnit5Test'.您将看到如下结果: 在此输入图像描述
  • 有关更多信息,您可以从github签出此项目.

UPDATE

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

dependencies {
    testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M3")
}
Run Code Online (Sandbox Code Playgroud)


dun*_*nni 10

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'
Run Code Online (Sandbox Code Playgroud)

http://junit.org/junit5/docs/current/user-guide/#running-tests-build

  • IntelliJ 2016.2现在支持JUnit5.只需使用@ Jean-FrançoisBeauchef编写的问题编译的build.gradle文件作为解决方案. (2认同)