11

我想配置 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)

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

4

3 回答 3

15

添加以下代码或 Maven 等效项:

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

说明:
ClassNamePatternFilterUtils 属于 platfrom-commons,它是传递依赖。该类在1.7.0版本中引入。因此,需要显式添加依赖项。

于 2020-10-16T07:30:10.757 回答
10

由于某些原因,您的项目构建路径 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'
于 2021-02-02T11:25:19.283 回答
3

我只使用以下方法解决了这个问题:

<dependency>
    <groupId>org.junit</groupId>
    <artifactId>junit-bom</artifactId>
    <version>5.7.0-M1</version>
    <type>pom</type>
</dependency>
于 2020-05-14T19:17:20.617 回答