我正在处理一个包含 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-03 中,项目的构建路径未明确包含 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
答案 0 :(得分:3)
如果我将 Maven 和 Gradle 的 JUnit 版本都更新到 5.7.1,问题就解决了。
当然,这仍然没有回答为什么它适用于较旧的 Eclipse 安装的问题。