13

I want to configure Maven to run Junit 5 tests using these dependencies:

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.7.0-M1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.7.0-M1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.7.0-M1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>1.7.0-M1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>3.3.3</version>
            <scope>test</scope>
        </dependency>

But I get exception:

"C:\Program Files\Java\jdk-14\bin\java.exe"
Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/platform/commons/util/ClassNamePatternFilterUtils
    at org.junit.platform.launcher.core.LauncherFactory.loadAndFilterTestExecutionListeners(LauncherFactory.java:113)
    at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:99)
    at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:72)
    at com.intellij.junit5.JUnit5IdeaTestRunner.createListeners(JUnit5IdeaTestRunner.java:46)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:31)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.commons.util.ClassNamePatternFilterUtils
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)

Do you know how I can solve this issue?

1
  • Please show the rest of your Maven pom file. First guess: s.t. else is pulling in a different version. What I can already see is that the first 4 deps can be replaced by a single one onto the aggregate artefact junit-jupiter. Commented May 14, 2020 at 19:08

4 Answers 4

17

Add below code or maven equivalent:

testRuntimeOnly "org.junit.platform:junit-platform-commons:1.7.0"

Explnation:
ClassNamePatternFilterUtils belongs to platfrom-commons which is transitive dependency. This class introduced in the 1.7.0 version. Hence, needs to explicity add the dependency.

Sign up to request clarification or add additional context in comments.

Comments

13

For some reason in your project build path org/junit/platform/commons/util/ClassNamePatternFilterUtils.class is missing, but this can be found in junit-platform-commons (1.7.0)

for maven projects add this dependency to pom.xml file :

<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-commons</artifactId>
    <version>1.7.0</version>
</dependency>

for gradle projects add this dependency to build.gradle file:

compile group: 'org.junit.platform', name: 'junit-platform-commons', version: '1.7.0'

2 Comments

For maven dependency, use 'test' scope
For gradle project, it works. Thank You.
4

I fixed the issue using only:

<dependency>
    <groupId>org.junit</groupId>
    <artifactId>junit-bom</artifactId>
    <version>5.7.0-M1</version>
    <type>pom</type>
</dependency>

Comments

0

I had same issue and resolved, by adding below two dependencies

    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-launcher</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-commons</artifactId>
    </dependency>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.