Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 966
Jean-François Beauchef Avatar asked Jul 10 '16 16:07

Jean-François Beauchef


People also ask

How do I run a JUnit test in IntelliJ Gradle?

In your Gradle project, in the editor, create or select a test to run. From the context menu, select Run <test name>. icon in the left gutter. If you selected the Choose per test option, IntelliJ IDEA displays both Gradle and JUnit test runners for each test in the editor.

Does IntelliJ support JUnit 5?

Learn more. Show activity on this post. From jetbrains blog: IntelliJ IDEA supports the ability to actually run tests written for JUnit 5 – there's no need to use the additional libraries (like the Gradle or Maven plugins for example), all you need is to include the JUnit 5 dependency.

How does Gradle run JUnit tests?

Test detection By default, Gradle will run all tests that it detects, which it does by inspecting the compiled test classes. This detection uses different criteria depending on the test framework used. For JUnit, Gradle scans for both JUnit 3 and 4 test classes.


1 Answers

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

like image 90
dunni Avatar answered Nov 16 '22 01:11

dunni