简体   繁体   English

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

[英]NoSuchFieldError when trying to run a jUnit test with Spring

So far I have two tests. 到目前为止,我有两个测试。 One uses only jUnit framework and works fine. 一个只使用jUnit框架并且工作正常。 The other one uses spring-test library and creates this exception every time I try to run it. 另一个使用spring-test库并在每次尝试运行时创建此异常。 Any ideas what may cause the problem? 什么可能导致问题的想法?

Error 错误

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 test dependencies 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>

Dependency tree 依赖树

[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

Are you using an older version of Eclipse (Galileo or before)? 您使用的是旧版本的Eclipse(Galileo还是以前版本)? or an older version of the junit plugin? 或junit插件的旧版本? If so, this may be the cause of the problem. 如果是这样,这可能是问题的原因。 ParentRunner is looking for Sorter.NULL, which was introduced in JUnit 4.5: 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;
        }});

If you don't have this bit of code, you're probably using a pre-4.5 version. 如果您没有这段代码,那么您可能正在使用4.5之前的版本。 On your Eclipse, do Ctrl-Shift-T and see if you have multiple versions of the Sorter class available, and if so, make sure neither of them are pre 4.5. 在Eclipse上,执行Ctrl-Shift-T并查看是否有多个版本的Sorter类可用,如果可用,请确保它们都不是4.5之前的版本。 Also, look in your project setup in your Build Path, and if there is JUnit entry (not the maven version), remove it, and try again. 另外,在Build Path中查看项目设置,如果有JUnit条目(不是maven版本),请将其删除,然后重试。

EDIT: This can also be caused by a transitive dependency of Maven. 编辑:这也可能是由Maven的传递依赖引起的。 Maybe one of your libraries has a dependency on a JUnit version which is pre-4.5. 也许您的某个库依赖于4.5之前的JUnit版本。

Eclipse Build Path,JUnit条目

I have solved this, changing to JUnit 4.10. 我已经解决了这个问题,改为JUnit 4.10。 The original exception was: 最初的例外是:

java.lang.NoSuchFieldError: NULL
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:54)
    at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:55)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl$1.<init>(JUnit45AndHigherRunnerImpl.java:23)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.<init>(JUnit45AndHigherRunnerImpl.java:23)
    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.mockito.internal.runners.util.RunnerProvider.newInstance(RunnerProvider.java:39)
    at org.mockito.internal.runners.RunnerFactory.create(RunnerFactory.java:28)
    at org.mockito.runners.MockitoJUnitRunner.<init>(MockitoJUnitRunner.java:57)
    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.buildRunner(ClassRequest.java:33)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:28)
    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)

I had exactly the same problem and I found out it was caused by a transitive dependency from org.jmock:jmock-junit4. 我有完全相同的问题,我发现它是由org.jmock:jmock-junit4的传递依赖引起的。 It was sorted when I replaced it with org.jmock:jmock. 当我用org.jmock:jmock替换它时它被排序了。

This looks like it's caused by the fact that you have multiple JUnit dependencies (of different versions) in your classpath. 这看起来像是由于您的类路径中有多个JUnit依赖项(不同版本)。 Check your classpath for that (if you're unsing maven do a mvn dependency:tree), and then get rid of the older one (again, if using maven, find out which directlly imported dependency imports in turn the older JUnit, and make an <exclusion> on that dependency for the JUnit sub-dependency in your pom.xml). 检查你的类路径(如果你没有注意maven做一个mvn依赖:树),然后摆脱旧的(再次,如果使用maven,找出哪个直接导入的依赖项导入反过来旧的JUnit,并使对pom.xml中JUnit子依赖项的依赖项的<exclusion> )。 Also you might want to update your actual JUnit dependency to the latest version which is (at least) 4.10. 此外,您可能希望将实际的JUnit依赖关系更新为(至少)4.10的最新版本。

I got this error due to the same reason. 由于同样的原因我得到了这个错误。 but in my case i was unable to see the different version of Junits just by looking into my pom.xml or maven source tree as my project was using transitive dependencies (between projects). 但在我的情况下,由于我的项目使用传递依赖项(项目之间),我无法通过查看我的pom.xml或maven源代码树来查看不同版本的Junits。 i,e project "A" has a dependency on project "B". 我,项目“A”依赖于项目“B”。 so project "A" directly referring to "B"(from the workspace). 所以项目“A”直接引用“B”(来自工作区)。 so when this happens, we will not be able to identify if the different version of junit exists in the dependency. 所以当发生这种情况时,我们将无法确定依赖项中是否存在不同版本的junit。 so what i did was, i closed the project "B". 所以我做的是,我关闭了项目“B”。 deleted project "A" from eclipse (not from the workspace). 从eclipse(而不是从工作区)删除项目“A”。 in the project "A" source directory, delete all the files except the pom, src and the svn supporting folder. 在项目“A”源目录中,删除除pom,src和svn支持文件夹之外的所有文件。 Re-import the file back into to eclipse. 将文件重新导入到eclipse中。 This resolved the issue. 这解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试运行单元测试时出现Junit错误 - Junit Error when trying to run unit test java.lang.NoSuchFieldError:运行JUnit测试时的EDGE - java.lang.NoSuchFieldError: EDGE when running JUnit test 当我“将 Spring 作为 JUnit 测试运行”时,内部会发生什么? - What happens internally when I "Run Spring as JUnit Test"? 使Spring JUnit Test在测试数据库上运行 - Make Spring JUnit Test run on test database 试图在Spring WebFlow Project的服务级别上运行junit测试。 假设$ AssumptionViolatedException - Trying to run junit test on service level of Spring WebFlow Project. Assume$AssumptionViolatedException 尝试使用 JUnit5 进行测试时出现 NullPointerException - NullPointerException when trying to test using JUnit5 在 spring 上运行持久性测试(JUnit)时出错 - error when running persistence test(JUnit) on spring Spring Boot JPA 元模型不能为空! 尝试运行 JUnit / 集成测试时 - Spring Boot JPA metamodel must not be empty! when trying to run JUnit / Integration Tests 我运行它时的JUnit测试错误 - JUnit test error when I run it 尝试使用 JUnit、Z4D114Z38BC079BE73B3 测试 Spring MVC controller 方法时出现 NullPointerException - NullPointerException while trying to test a Spring MVC controller method with JUnit, Mockito
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM