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

发布于 2024-12-08 16:47:26 字数 4192 浏览 0 评论 0原文

到目前为止我有两个测试。一种仅使用 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

So far I have two tests. One uses only jUnit framework and works fine. The other one uses spring-test library and creates this exception every time I try to run it. 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

<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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

童话里做英雄 2024-12-15 16:47:26

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

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 条目

Are you using an older version of Eclipse (Galileo or before)? or an older version of the junit plugin? If so, this may be the cause of the problem. ParentRunner is looking for Sorter.NULL, which was introduced in JUnit 4.5:

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. 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. 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.

EDIT: This can also be caused by a transitive dependency of Maven. Maybe one of your libraries has a dependency on a JUnit version which is pre-4.5.

Eclipse Build Path, JUnit entry

萌无敌 2024-12-15 16:47:26

我已经解决了这个问题,更改为 JUnit 4.10。最初的例外是:

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 have solved this, changing to 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)
羞稚 2024-12-15 16:47:26

这看起来是由于类路径中有多个 JUnit 依赖项(不同版本)造成的。检查你的类路径(如果你取消 maven 执行 mvn dependency:tree),然后删除旧的(同样,如果使用 maven,找出哪些直接导入的依赖项依次导入旧的 JUnit,然后使pom.xml 中 JUnit 子依赖项的 )。此外,您可能希望将实际的 JUnit 依赖项更新到最新版本(至少)4.10。

This looks like it's caused by the fact that you have multiple JUnit dependencies (of different versions) in your classpath. 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). Also you might want to update your actual JUnit dependency to the latest version which is (at least) 4.10.

听闻余生 2024-12-15 16:47:26

我遇到了完全相同的问题,我发现它是由 org.jmock:jmock-junit4 的传递依赖引起的。当我用 org.jmock:jmock 替换它时它已经排序了。

I had exactly the same problem and I found out it was caused by a transitive dependency from org.jmock:jmock-junit4. It was sorted when I replaced it with org.jmock:jmock.

っ左 2024-12-15 16:47:26

由于同样的原因,我收到了此错误。但就我而言,我无法通过查看我的 pom.xml 或 maven 源代码树来查看 Junits 的不同版本,因为我的项目正在使用传递依赖项(项目之间)。即项目“A”依赖于项目“B”。因此项目“A”直接引用“B”(来自工作区)。因此,当发生这种情况时,我们将无法识别依赖项中是否存在不同版本的 junit。所以我所做的是,我关闭了项目“B”。从 eclipse(而不是从工作区)删除了项目“A”。在项目“A”源目录中,删除除pom、src和svn支持文件夹之外的所有文件。将文件重新导入回 eclipse。这解决了这个问题。

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). i,e project "A" has a dependency on project "B". so project "A" directly referring to "B"(from the workspace). so when this happens, we will not be able to identify if the different version of junit exists in the dependency. so what i did was, i closed the project "B". deleted project "A" from eclipse (not from the workspace). in the project "A" source directory, delete all the files except the pom, src and the svn supporting folder. Re-import the file back into to eclipse. This resolved the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文