Gradle project running jUnit 5 tests in IntelliJ

Jean-François Beauchef picture Jean-François Beauchef · Jul 10, 2016 · Viewed 10.7k times · Source

I am trying both Gradle and jUnit5 right now. Everything works fine except that I cannot run a specific jUnit test. The "Run 'SampleTest'" option does not appear when I right-click a test class.

I have the latest version of IntelliJ (2016.1.3) Ultimate. Here is my build.gradle file:

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'
}

The project structure is the standard one (like in Maven). And here is an example of a test:

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);
  }
}

What am I missing?

EDIT:

It seems that Gradle is not picking up my test either. When I go to build/reports/tests/index.html, it indicates 0 test.

FINAL EDIT:

Following @dunny's answer, here is what I did to make everything work. I modified my build.gradle file like this:

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'
    }
}

In IntelliJ, I then opened the Gradle window, and clicked on the "refresh all gradle projects" button, to refresh the libraries.

Then in my test class, I added @RunWith(JUnitPlatform.class) on top of the class declaration.

And when I do a gradle build, the results are available here: build\test-results\junit-platform\TEST-junit-jupiter.xml

Answer

walsh picture walsh · Jul 21, 2016

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: enter image description here
  • 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")
}