Gradle project running jUnit 5 tests in IntelliJ

落爺英雄遲暮 提交于 2019-12-01 03:02:57

IntelliJ 2016.1.3 doesn't have support for JUnit 5 tests. You can however add the annotation @RunWith(JUnitPlatform.class), which would execute your test in a JUnit 4 compatibility mode (you can still use all JUnit 5 features). See http://junit.org/junit5/docs/current/user-guide/#running-tests-junit-platform-runner for more information.

For Gradle you need to include the Gradle JUnit 5 plugin to enable support:

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'

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

The latest Idea 2016.2 supports JUnit 5 framework now. You can directly run JUnit5 test without junit-gradle-plugin any more. Please see WHAT'S NEW IN INTELLIJ IDEA. After you upgraded your Idea to this new version, You can create a gradle project and do following steps to test how to run JUnit 5 test.

  • 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.
    }
    
  • Create a class FirstJUnit5Test in your test source folder

    import org.junit.jupiter.api.Test;
    import static org.junit.jupiter.api.Assertions.assertEquals;
    
    public class FirstJUnit5Test {
        @Test
        void myFirstTest() {
            assertEquals(2, 1 + 1);
        }
    }
    
  • Right click on this test class in the left project pane, and then select "Run 'FirstJUnit5Test'. You will see the result as following:

  • For more information, you can checkout this project from github.

UPDATE

For IDEA 2016.3.3 and higher, the dependencies configuration can be simplified to:

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

You have to run your tests with the JUnit 4 runner, since IntelliJ 2016.1.3 doesn't have JUnit 5 test runner.

If you are starting from the example pom.xml linked in the documentation, https://github.com/junit-team/junit5-samples/blob/r5.0.0-M1/junit5-maven-consumer/pom.xml, then do the following two things.

Add one more dependency

    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-runner</artifactId>
        <version>${junit.platform.version}</version>
    </dependency>

then make your test class public and annotate it with @RunWith(JUnitPlatform.class).

Your IDE will now recognize the class as a test and will provide integration.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!