Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to run junit-5 test cases from intellij idea

Junit-5 test cases run for me from command line successfully. I am using Maven and when I run using mvn clean install test cases run successfully but when to run individual test cases using IntelliJ it fails with the following error.

Feb 15, 2018 11:33:41 PM org.junit.jupiter.engine.discovery.JavaElementsResolver resolveMethod
WARNING: Method 'public static java.util.Collection com.softiventure.dtos.junit.parametrized.NestedParametrizedTest$sumTest.parameters()' could not be resolved.
Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.launcher.Launcher.execute(Lorg/junit/platform/launcher/LauncherDiscoveryRequest;)V
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:59)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

My sample test class is :

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@DisplayName("Pass paramters ")
public class ParametrizedJunit5Test {

    @DisplayName("Should pass a non-null message to our test method")
    @ParameterizedTest
    @ValueSource(strings = {"Hello", "World"})
    void shouldPassNonNullMessageAsMethodParameter(String message) {
        assertNotNull(message);
    }
}

Any help would be much appreciated.

like image 374
Vinay Prajapati Avatar asked Feb 15 '18 18:02

Vinay Prajapati


2 Answers

There are some compatability issues between JUnit5 and IntelliJ.

According to the JUnit docs ...

it is recommended to use IDEA 2017.3 or newer since these newer versions of IDEA will download the following JARs automatically based on the API version used in the project: junit-platform-launcher, junit-jupiter-engine, and junit-vintage-engine.

This blog post from the IntelliJ team is also relevant. There's a comment on that post from a user who encountered the same error as you and that user's response is:

I update idea to 2017.2.6, it works well.

So, update to IntelliJ 2017.3 or newer and this issue should disappear.

like image 133
glytching Avatar answered Oct 03 '22 04:10

glytching


From offical comment, you may have to upgrade your IDE version

IDE has compilation dependency on the old junit 5 launcher jar and it is not compatible with current released version. So you have a choice to update IDE so it will be compatible with the junit version you use or to downgrade the junit version (check what version was bundled in IDEA_INSTALLATION_HOME/plugins/junit/lib). 2017.1 had only experimental support for junit 5 as junit 5 was not released yet at that time. Sorry for the inconvenience.

https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000791190-Intellij-does-not-run-Junit5-tests

like image 24
miskender Avatar answered Oct 03 '22 04:10

miskender