12

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?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • 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. – johanneslink May 14 '20 at 19:08

3 Answers3

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.

fcdt
  • 2,371
  • 5
  • 14
  • 26
Saurabh Trivedi
  • 221
  • 3
  • 6
12

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'
Sohan
  • 121
  • 1
  • 3
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>
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808