Jean-François Beauchef Jean-François Beauchef - 4 months ago 141
Java Question

Gradle project running jUnit 5 tests in IntelliJ

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

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