Junit5 with IntelliJ and Gradle

77,696

Solution 1

Adding specific dependencies solve the problem.

NOTE: UPDATE INTELLIJ ABOVE 2017.2.0 AS THERE WAS A BUG WITH THE JUNIT LAUNCHER

OXYGEN if you using eclipse.


Below dependency enables Junit5 parametrized tests which can be used instead of a DataProvider.

"org.junit.jupiter:junit-jupiter-params:5.0.0"
//for JUnit5 parametrized tests.

Junit5 API.

"org.junit.jupiter:junit-jupiter-api:5.0.0"
//JUnit5 API

Needed if you want to run legacy JUnit4 tests without changing the syntax and imports.

"org.junit.vintage:junit-vintage-engine:4:12.0"
//for legacy JUnit4 tests

EDIT: 07/2018 Match the version of the vintage runner to the jupiter version


Needed if you want to run JUnit5 tests with new syntax and imports.

"org.junit.jupiter:junit-jupiter-engine:5.0.0"
//for JUnit5 tests

java.lang.NoSuchMethodError: org.junit.platform.engine.EngineDiscoveryRequest.getDiscoveryFiltersByType(Ljava/lang/Class;)Ljava/util/List;


Launcher.

"org.junit.platform:junit-platform-launcher:1.0.0
//to handle default launcher

Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.getDefaultClassLoader()Ljava/lang/ClassLoader;


Additional info how to install JUnit5


Since version 4.6 for Gradle, there is no need for plugins anymore Gradle supports Junit5 natively just do: And the version of the vintage runner is now same as the JUnit 5 version.

dependencies {

    testImplementation "org.junit.jupiter:junit-jupiter-params:$junitVersion"
    testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"

    testRuntimeOnly "org.junit.vintage:junit-vintage-engine:$junitVersion"
    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
}

test {  
    useJUnitPlatform {
        includeEngines 'junit-jupiter', 'junit-vintage'
    }
}

Solution 2

The configuration I use is below.

The vintage engine dependency is only required if you are using junit4 tests as well.

The jupiter params is only required if using parameterised tests.

<properties>
    <junit.version>5.0.0</junit.version>
</properties>
...
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>1.0.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>4.12.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

Solution 3

I have to change the version of JUnit from 5.4.0 for 5.3.2 and it works like a charm.

Solution 4

I had a problem using IntelliJ and jupiter in a corporate environment. I was able to run 'mvn test', but starting tests in IntelliJ prompts 'IntelliJ failed to resolve junit platform launcher 1.5 2'

Configuring a HTTP Proxy fixed this for me:

Settings -> Appearance & Behavior -> Systems Settings -> HTTP Proxy -> Manual proxy configuration

Share:
77,696
LazerBanana
Author by

LazerBanana

Curious developer with Java as a core language. Full-stack, one-man team. Music producer after hours. Lasers are cool. Bananas are cool.

Updated on June 05, 2021

Comments

  • LazerBanana
    LazerBanana almost 3 years

    Trying to migrate my project to java8 + Junit5 using IntelliJ 2017.2

    I have added junit-jupiter-api version 5.0.0-M6

    and junit-platform-launcher version 1.0.0-M6

    Project structure is a default maven convention src/test/java

    Found a couple articles about this but none of them did solve my issue.

    It runs nicely in a console, I presume this is something to do with the IntelliJ default JUnit Runner, or I am missing some dependencies?


    When I Run a single test class all works fine but when I select the directory and Run all 'Tests' in Java like I used to do then I encounter few errors.

    WARNING: TestEngine with ID 'junit-jupiter' failed to discover tests
    java.lang.NoSuchMethodError: org.junit.platform.engine.TestDescriptor.pruneTree()V
    
    Aug 02, 2017 2:44:56 PM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
    WARNING: TestEngine with ID 'junit-vintage' failed to discover tests
    java.lang.NoSuchMethodError: org.junit.platform.engine.EngineDiscoveryRequest.getDiscoveryFiltersByType(Ljava/lang/Class;)Ljava/util/List;
    
    Aug 02, 2017 2:44:56 PM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
    WARNING: TestEngine with ID 'junit-jupiter' failed to discover tests
    java.lang.NoSuchMethodError: org.junit.platform.engine.TestDescriptor.pruneTree()V
    
    Aug 02, 2017 2:44:56 PM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
    WARNING: TestEngine with ID 'junit-vintage' failed to discover tests
    java.lang.NoSuchMethodError: org.junit.platform.engine.EngineDiscoveryRequest.getDiscoveryFiltersByType(Ljava/lang/Class;)Ljava/util/List;
    

    Note: I have not migrated any tests yet, all are Junit 4 syntax.

  • aenw
    aenw almost 6 years
    Thanks! Your config worked for me. (I'm using intelliJ 2018.1)
  • Sibish
    Sibish almost 6 years
    2017.2.0 have the bug as well?
  • Zon
    Zon over 5 years
    Could not find method test() for arguments ... Where to put test block?
  • LazerBanana
    LazerBanana over 5 years
    Applied java or android plugin? Test task come through those plugins.
  • mojtab23
    mojtab23 almost 5 years
    Adding junit-platform-launcher solved my problem.
  • Dracontis
    Dracontis over 4 years
    Same issue with JUnit 5.5.2.
  • Mikhail Geyer
    Mikhail Geyer about 4 years
    The same for 5.5.1 too.
  • Neon Warge
    Neon Warge about 3 years
    Same with 5.6.2