java - 无法在 Eclipse 2021-03 上运行 JUnit 5 测试

标签 java eclipse testing junit junit5

我正在开发一个包含 Gradle 和 Maven 子项目的项目。直到昨天我还在使用 Eclipse 2020-03,但我更新到 2021-03,现在我无法运行任何测试。我没有对项目的配置进行任何更改,junit 5.6.0 都包含在 pom.xml 和 build.gradle 中。

现在每当我尝试运行测试时,都会收到此错误:

java.lang.NoClassDefFoundError: org/junit/platform/commons/util/ClassNamePatternFilterUtils
at org.junit.platform.launcher.core.LauncherFactory.loadAndFilterTestExecutionListeners(LauncherFactory.java:122)
at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:108)
at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:75)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.<init>(JUnit5TestLoader.java:34)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at java.base/java.lang.Class.newInstance(Class.java:584)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createRawTestLoader(RemoteTestRunner.java:371)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createLoader(RemoteTestRunner.java:366)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.defaultInit(RemoteTestRunner.java:310)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.init(RemoteTestRunner.java:225)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.commons.util.ClassNamePatternFilterUtils
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 14 more

当我将 Junit 5 手动添加到项目的构建路径时,正如此处许多类似问题所建议的那样,我在“添加库”窗口中得到了以下信息:

Current Location: org.junit.jupiter.api_5.7.1.v20210222-1948.jar - /Users/user/.p2/pool/plugins 

但即便如此,当我添加它时,我发现 junit 启动器未找到,然后是:

java.lang.SecurityException: class "org.junit.platform.commons.util.ClassNamePatternFilterUtils"'s signer information does not match signer information of other classes in the same package

请注意,在 2020 年 3 月,项目的构建路径未明确包含 Junit 5(未手动添加)。在 Maven 和 Gradle 项目中,相同的测试也按预期工作。

我还尝试删除与以前的 eclipse 安装相关的任何内容(~/Library 相关内容)

我的.classpath如下:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" output="bin/main" path="src/main/java">
        <attributes>
            <attribute name="gradle_scope" value="main"/>
            <attribute name="gradle_used_by_scope" value="main,test"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="src" output="bin/main" path="src/main/resources">
        <attributes>
            <attribute name="gradle_scope" value="main"/>
            <attribute name="gradle_used_by_scope" value="main,test"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="src" output="bin/test" path="src/test/java">
        <attributes>
            <attribute name="gradle_scope" value="test"/>
            <attribute name="gradle_used_by_scope" value="test"/>
            <attribute name="test" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="src" output="bin/test" path="src/test/resources">
        <attributes>
            <attribute name="gradle_scope" value="test"/>
            <attribute name="gradle_used_by_scope" value="test"/>
            <attribute name="test" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11/"/>
    <classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
    <classpathentry combineaccessrules="false" kind="src" path="/com.myproject.businesslayer"/>
    <classpathentry kind="output" path="bin/default"/>
</classpath>

我的根 pom.xml 的相关部分:

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <tycho-version>1.7.0</tycho-version>
        <mockito-version>3.3.0</mockito-version>
        <hamcrest-version>2.1</hamcrest-version>
        <archunit-version>0.11.0</archunit-version>
        <junit-version>5.6.0</junit-version>
        <xmlunit-version>2.6.3</xmlunit-version>

        <!-- plugin versions -->
        <compiler-plugin-version>3.8.0</compiler-plugin-version>
        <groovy-eclipse-compiler-version>3.4.0-01</groovy-eclipse-compiler-version>
        <jar-plugin-version>3.1.2</jar-plugin-version>
        <surefire-plugin-version>2.22.2</surefire-plugin-version>
        <versions-plugin-version>2.7</versions-plugin-version>
        <jacoco-plugin-version>0.8.4</jacoco-plugin-version>
        <sonar-plugin-version>3.8</sonar-plugin-version>
        <groovy-eclipse-batch-version>2.5.8-01</groovy-eclipse-batch-version>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    </properties>
    <modules>
        ...
    </modules>

    <build>
        <defaultGoal>verify</defaultGoal>
        <!-- only relevant for modules containing plain java tests -->
        <sourceDirectory>src</sourceDirectory>
        <testSourceDirectory>test</testSourceDirectory>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${surefire-plugin-version}</version>
                    <dependencies>
                        <dependency>
                            <groupId>org.junit.jupiter</groupId>
                            <artifactId>junit-jupiter-engine</artifactId>
                            <version>${junit-version}</version>
                        </dependency>
                    </dependencies>
                    <executions>
                        <execution>
                            <id>test</id>
                            <phase>test</phase>
                            <configuration>
                                <includes>
                                    <include>**/*.java</include>
                                </includes>
                                <excludedGroups>PluginTest</excludedGroups>
                            </configuration>
                            <goals>
                                <goal>test</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <!-- only for plain java test execution -->
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit-version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>${junit-version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.tngtech.archunit</groupId>
            <artifactId>archunit-junit5-api</artifactId>
            <version>${archunit-version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.tngtech.archunit</groupId>
            <artifactId>archunit-junit5-engine</artifactId>
            <version>${archunit-version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>${mockito-version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>${hamcrest-version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
            <version>${hamcrest-version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>${org.assertj}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>


</project>

以及我的根 build.gradle:

plugins {
...
}

subprojects { sub ->

    apply plugin: 'java'

    sourceCompatibility = 11
    targetCompatibility = 11
    
    tasks.withType(JavaCompile) {
        options.encoding = 'UTF-8'
    }
    
    tasks.withType(Test) {
      ...
   }

    dependencies {
        testCompile "org.assertj:assertj-core:${assertJVersion}"
        testCompile "org.junit.jupiter:junit-jupiter-api:${jUnitVersion}"
        testRuntime "org.junit.jupiter:junit-jupiter-engine:${jUnitVersion}"
        testCompile "org.junit.jupiter:junit-jupiter-params:${jUnitVersion}"

        testCompile "org.mockito:mockito-core:${mockitoVersion}"
        testCompile "org.hamcrest:hamcrest:${hamcrestVersion}"
        testCompile "org.slf4j:slf4j-simple:${slf4jVersion}"

        testCompile "com.tngtech.archunit:archunit-junit5-api:${archUnitVersion}"
        testRuntime "com.tngtech.archunit:archunit-junit5-engine:${archUnitVersion}"
    }

    test {
        useJUnitPlatform()
        finalizedBy jacocoTestReport
    }
    
    jacocoTestReport {
        dependsOn test
    }

    repositories {
        mavenLocal()
        jcenter()
        maven {
            url "${nexusUrl}"
        }
    }
}

wrapper {
    gradleVersion = '5.6'
}

在 gradle.properties 中:

hamcrestVersion=2.1
jUnitVersion=5.6.0
archUnitVersion=0.11.0

最佳答案

如果我将 Maven 和 Gradle 的 JUnit 版本更新到 5.7.1,问题就解决了。

当然,这仍然没有回答为什么它可以在较旧的 Eclipse 安装上运行的问题。

关于java - 无法在 Eclipse 2021-03 上运行 JUnit 5 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66984464/

相关文章:

java - 用java获取Mysql数据库创建日期

java - Preg_match PHP 到 java 翻译

java - 方法引用的组成

ruby - 如何在有和没有javascript的情况下运行 cucumber 场景避免代码重复

java - 使用 GUICE 注入(inject) HttpClient 以在 Java 中获取模拟响应

eclipse - 降低 Eclipse(类型)警告级别

java - 我从哪里开始调查不会结束的 Java 进程?

symfony - PhpStorm + PHPUnit 故障未显示在测试选项卡中

c++ - GTest改变失败的输出

java - pom.xml 配置文件中的依赖项