Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoSuchMethodError when run test with Junit 5

Tags:

java

junit5

I created a simple test to try Junit 5:

import org.junit.jupiter.api.Test;
public class MyTest {

    @Test
    public void testJupiter() {
        System.out.println("test");
    }
}

This is the dependency I use:

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.0.0</version>
            <scope>test</scope>
        </dependency>

Stack trace is the next:

Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.getDefaultClassLoader()Ljava/lang/ClassLoader;
at org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry.loadTestEngines(ServiceLoaderTestEngineRegistry.java:30)
    at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:53)
    at com.intellij.junit5.JUnit5IdeaTestRunner.createListeners(JUnit5IdeaTestRunner.java:39)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:49)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Any ideas what went wrong?

like image 240
IKo Avatar asked Sep 23 '17 21:09

IKo


2 Answers

I see that you are using IntelliJ which supports just specific versions of jUnit5.

Junit5 - running tests in IntelliJ

Try e.g.

<version>5.0.0-M4</version>
like image 141
shippi Avatar answered Nov 14 '22 12:11

shippi


Besides the dependency, did you also configure the JUnit5 plugin in your pom.xml? It should look like this (taken from the docs):

...
<build>
    <plugins>
        ...
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.0.0</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.0.0</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>
...
<dependencies>
    ...
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.0.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>
...
like image 1
Daniel Avatar answered Nov 14 '22 12:11

Daniel