Java Can';t在Eclipse2021-03上运行JUnit5测试

Java Can';t在Eclipse2021-03上运行JUnit5测试,java,eclipse,testing,junit,junit5,Java,Eclipse,Testing,Junit,Junit5,我正在从事一个包括Gradle和Maven子项目的项目。直到昨天我还在使用Eclipse2020-03,但我更新到2021-03,现在我无法运行任何测试。我没有对项目的配置做任何更改,junit5.6.0包含在pom.xml和build.gradle中 现在,每当我尝试运行测试时,都会出现以下错误: java.lang.NoClassDefFoundError: org/junit/platform/commons/util/ClassNamePatternFilterUtils at org.

我正在从事一个包括Gradle和Maven子项目的项目。直到昨天我还在使用Eclipse2020-03,但我更新到2021-03,现在我无法运行任何测试。我没有对项目的配置做任何更改,junit5.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启动器,接下来是:

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年,项目的构建路径没有明确包括JUnit5(没有手动添加)。同样的测试也可以在Maven和Gradle项目中按预期工作

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

My.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>
在gradle.properties中:

hamcrestVersion=2.1
jUnitVersion=5.6.0
archUnitVersion=0.11.0

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


当然,这仍然不能回答为什么它在旧的Eclipse安装上工作的问题。

当您通过Eclipse安装程序安装Eclipse时,您通过删除
.p2/pool/plugins
@howlger Ok中的文件破坏了所有Eclipse安装,但现在我尝试重新安装2020-03和2021-03,同样的情况也发生了。测试将在2020年运行,但不会在2021年运行!显示您的
.classpath
pom.xml
/
build.gradle
文件。@howlger我现在已经将它们添加到我的帖子中了!这确实有所帮助——在我的例子中,eclipse仍然使用旧库。我必须同步和清理项目。如果使用了正确的版本,则应签入“项目和外部依赖项”。
    <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>
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'
}
hamcrestVersion=2.1
jUnitVersion=5.6.0
archUnitVersion=0.11.0