Gradle project running jUnit 5 tests in IntelliJ

11,589

Solution 1

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

Solution 2

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")
}
Share:
11,589
Jean-François Beauchef
Author by

Jean-François Beauchef

Web developer specialized in Java (J2EE, Spring, JSF, Hibernate) and PHP (Symfony, Doctrine). I am also a music enthusiast who produces his own electronic music (Apple Logic, Ableton Live).

Updated on July 15, 2022

Comments

  • Jean-François Beauchef
    Jean-François Beauchef almost 2 years

    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

  • Jean-François Beauchef
    Jean-François Beauchef almost 8 years
    Thanks, but everything was already said by dunni, except for the pom.xml part, but I am not using Maven. I am using Gradle. Thanks anyway.
  • user7610
    user7610 almost 8 years
    @Jean and making the test class public. JUnit5 can work without it, JUnit4 doesn't. Switching the dependency declaration into Gradle syntax should be trivial for a Gradle user.
  • Jean-François Beauchef
    Jean-François Beauchef almost 8 years
    But my test class is public?
  • user7610
    user7610 almost 8 years
    It wouldn't have to be if you were using Junit5 with a Junit5 runner...
  • Jean-François Beauchef
    Jean-François Beauchef almost 8 years
    Ah ok, I see. But I was trying to understand what was wrong with what I was doing. But thanks anyway.
  • Yngvar Kristiansen
    Yngvar Kristiansen almost 8 years
    IntelliJ 2016.2 now has support for JUnit5. Just use the build.gradle file edited into the question by @Jean-FrançoisBeauchef as the solution.