Junit5 with IntelliJ and Gradle

Viewed 84447

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.

8 Answers

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

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

for android studio if you encounter

Unable to resolve org.junit.jupiter:junit-jupiter-api

go to your file ..\AndroidStudioProjects\Pets\app\build.gradle

at the dependencies section

input these two lines of code

 testImplementation 'junit:junit:4.12'
 androidTestImplementation 'org.junit.jupiter:junit-jupiter-api:5.0.0-M4'

I had same problem with junit 5.6.1 and bumping it to 5.7.0 fixed it:

testImplementation "org.junit.jupiter:junit-jupiter-api:5.7.0"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.7.0"

If you are setting behind a proxy server and maven clean install works fine(but not intellij), then most likely, it's proxy related. Here is what fixed it for me. Choosing proxy auto detection.

enter image description here

Official docs:

Maven Surefire and Maven Failsafe can run JUnit 4 based tests alongside Jupiter tests as long as you configure test scoped dependencies on JUnit 4 and the JUnit Vintage TestEngine implementation similar to the following.

<!-- ... -->
<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>
        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>
    </plugins>
</build>
<!-- ... -->
<dependencies>
    <!-- ... -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>5.7.2</version>
        <scope>test</scope>
    </dependency>
    <!-- ... -->
</dependencies>
<!-- ... -->
Related