【问题标题】:Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/platform/commons/util/ClassNamePatternFilterUtils线程“主”java.lang.NoClassDefFoundError 中的异常:org/junit/platform/commons/util/ClassNamePatternFilterUtils
【发布时间】:2020-08-31 05:13:33
【问题描述】:

我想配置 Maven 以使用这些依赖项运行 Junit 5 测试:

        <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>

但我得到了例外:

"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)

你知道我该如何解决这个问题吗?

【问题讨论】:

  • 请显示您的 Maven pom 文件的其余部分。第一个猜测:s.t. else 正在引入不同的版本。我已经看到的是,前 4 个 deps 可以在聚合人工制品 junit-jupiter 上替换为单个 deps。

标签: java junit noclassdeffounderror junit5 junit-jupiter


【解决方案1】:

添加以下代码或 maven 等效代码:

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

解释:
ClassNamePatternFilterUtils 属于传递依赖的 platfrom-commons。该类在1.7.0版本中引入。因此,需要显式添加依赖。

【讨论】:

    【解决方案2】:

    由于某些原因,您的项目构建路径 org/junit/platform/commons/util/ClassNamePatternFilterUtils.class 缺失,但这可以在 junit-platform-commons (1.7.0) 中找到

    对于 maven 项目,将此依赖项添加到 pom.xml 文件中:

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

    对于 gradle 项目,将此依赖项添加到 build.gradle 文件中:

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

    【讨论】:

    • 对于maven依赖,使用'test'范围
    【解决方案3】:

    我仅使用以下方法解决了问题:

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

    【讨论】:

      猜你喜欢
      • 2021-10-16
      • 2013-09-29
      • 1970-01-01
      • 2013-03-19
      • 2017-08-13
      • 2017-04-14
      • 1970-01-01
      • 2019-10-17
      • 1970-01-01
      相关资源
      最近更新 更多