尝试使用Spring运行jUnit测试时出现NoSuchFieldError

用户219882:

到目前为止,我有两个测试。一个仅使用jUnit框架即可正常工作。另一个使用spring-test库并在每次尝试运行该异常时创建此异常。有什么想法可能导致问题吗?

错误

java.lang.NoSuchFieldError: NULL
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:48)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:59)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:104)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:27)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:32)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:41)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:31)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Maven测试依赖

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.7</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>${org.springframework.version}</version>
    <scope>test</scope>
</dependency>

依赖树

[INFO] [dependency:tree {execution: default-cli}]
[INFO] fake:war:1.0-SNAPSHOT
[INFO] +- log4j:log4j:jar:1.2.16:compile
[INFO] +- org.springframework:spring-web:jar:3.0.5.RELEASE:compile
[INFO] |  +- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  +- org.springframework:spring-beans:jar:3.0.5.RELEASE:compile
[INFO] |  +- org.springframework:spring-context:jar:3.0.5.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-aop:jar:3.0.5.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-expression:jar:3.0.5.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-asm:jar:3.0.5.RELEASE:compile
[INFO] |  \- org.springframework:spring-core:jar:3.0.5.RELEASE:compile
[INFO] |     \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] +- commons-codec:commons-codec:jar:1.4:compile
[INFO] +- org.glassfish:javax.faces:jar:2.1.3:compile
[INFO] +- org.richfaces.ui:richfaces-components-ui:jar:4.0.0.Final:compile
[INFO] |  +- org.richfaces.ui:richfaces-components-api:jar:4.0.0.Final:compile
[INFO] |  \- org.richfaces.core:richfaces-core-api:jar:4.0.0.Final:compile
[INFO] +- org.richfaces.core:richfaces-core-impl:jar:4.0.0.Final:compile
[INFO] |  +- net.sourceforge.cssparser:cssparser:jar:0.9.5:compile
[INFO] |  |  \- org.w3c.css:sac:jar:1.3:compile
[INFO] |  \- com.google.guava:guava:jar:r08:compile
[INFO] +- org.hibernate:hibernate-validator:jar:4.2.0.Final:compile
[INFO] |  \- org.slf4j:slf4j-api:jar:1.6.1:compile
[INFO] +- javax.validation:validation-api:jar:1.0.0.GA:compile
[INFO] +- javax.xml.bind:jaxb-api:jar:2.2.2:compile
[INFO] |  +- javax.xml.stream:stax-api:jar:1.0-2:compile
[INFO] |  \- javax.activation:activation:jar:1.1:compile
[INFO] +- com.sun.xml.bind:jaxb-impl:jar:2.2.2:compile
[INFO] +- javax.servlet:jstl:jar:1.1.2:compile
[INFO] +- junit:junit:jar:4.7:test
[INFO] +- org.springframework:spring-test:jar:3.0.5.RELEASE:test
[INFO] +- javax.servlet:jsp-api:jar:2.0:provided
[INFO] \- javax.servlet:servlet-api:jar:2.4:provided
马修·法威尔(Matthew Farwell):

您是否正在使用旧版本的Eclipse(Galileo或更低版本)?还是旧版的junit插件?如果是这样,这可能是问题的原因。ParentRunner正在寻找在JUnit 4.5中引入的Sorter.NULL:

package org.junit.runner.manipulation;

public class Sorter implements Comparator<Description> {
    /**
     * NULL is a <code>Sorter</code> that leaves elements in an undefined order
     */
    public static Sorter NULL= new Sorter(new Comparator<Description>() {
        public int compare(Description o1, Description o2) {
            return 0;
        }});

如果您没有这些代码,则可能使用的是4.5之前的版本。在Eclipse上,执行Ctrl-Shift-T组合键,然后查看是否有可用的Sorter类的多个版本,如果有,请确保两个版本都不在4.5之前。另外,在“构建路径”中查看项目设置,如果有JUnit条目(不是Maven版本),请将其删除,然后重试。

编辑:这也可能是由Maven的传递依赖引起的。也许您的一个库依赖于4.5之前的JUnit版本。

Eclipse构建路径,JUnit条目

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

java.lang.NoSuchFieldError: 运行 JUnit 测试时的边缘

使用Ant运行JUnit测试时出现ClassNotFoundException

运行junit测试时出现ZipException

尝试运行JUnit 5测试时出现java.lang.NoClassDefFoundError

尝试运行Junit测试时出现java.lang.IllegalArgumentException错误

尝试运行JUnit测试时,为什么会出现“在使用ClassLoader搜索持久性归档时抛出异常”错误?

在启动Spring JUnit测试时出现IllegalStateException

运行Gradle JUnit测试时出现“地址已在使用中:绑定”异常

使用JUnit运行黄瓜测试时出现java.lang.NoClassDefFoundError异常

使用play2框架运行JUnit测试时,mysql连接出现问题

尝试使用Maven运行Mockito junit5测试时,java.lang.NoClassDefFoundError:org / junit / platform / commons / PreconditionViolationException

尝试使用Node运行测试Neo4J文件时出现Error.captureStackTrace

在 Maven 中运行 JUnit 测试时出现 ClassNotFoundException

当我尝试运行测试时出现错误

尝试在Play中运行Mockito测试时出现错误消息

使用Spring Boot,Selenium和JUnit运行Cucumber测试时获取NullPointerException

尝试运行 spring boot 时出现 JsonParseException

使用MockMvc和JUnit 5测试LocalDate时出现AssertionError

运行Spring MVC测试时出现NoSuchMethod错误

运行单元测试时出现Spring Boot错误

尝试在测试中使用方法时出现NullPointer错误

尝试使用 VNC 查看 Behat 测试时出现连接错误

使用 ApplicationContext 运行 JUnit 测试时禁用 Hazelcast 实例

在SpringBoot项目上使用DBUNIT运行Junit测试时出错

尝试在Intellij 2018.2.RC中执行JUnit 4测试时出现NoClassDefFoundError

尝试使用pip时出现运行时错误

使用SBT运行JUnit测试

使用IntelliJ运行JUnit测试

使用@Configuration时运行单元测试时出现NoSuchBeanDefinitionException