Spring JUnit:java.lang.NoSuchFieldError:NULL

时间:2012-07-15 05:02:42

标签: spring junit4

我对maven很新。当我执行mvn clean install时,我在执行期间的所有测试类中都会出现以下错误。在浏览了SO上的一些帖子并通过Google搜索之后,所有提到的JUnit版本都出现了问题。我交叉检查了所有的pom文件,他们有junit版本4.8.2。但是,我仍然继续犯这个错误。

<testcase time="0.002" classname="com.service.ServiceImpl" name="initializationError0">
<error message="NULL" type="java.lang.NoSuchFieldError">java.lang.NoSuchFieldError: NULL
    at org.junit.runners.ParentRunner.&lt;init&gt;(ParentRunner.java:48)
    at org.junit.runners.BlockJUnit4ClassRunner.&lt;init&gt;(BlockJUnit4ClassRunner.java:58)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.&lt;init&gt;(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.buildRunner(ClassRequest.java:33)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:28)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:51)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:120)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:103)
    at org.apache.maven.surefire.Surefire.run(Surefire.java:169)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350)
    at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021)
</error>
</testcase>

我运行了mvn dependency:tree并搜索了所有junit实例。所有这些都显示了4.8.2。

1 个答案:

答案 0 :(得分:5)

这是jmock-junit4的问题。它通过放置以下来删除(导入jmock-junit时不包括junit) -

<dependency>
    <groupId>org.jmock</groupId>
    <artifactId>jmock-junit4</artifactId>
    <version>2.5.1</version>
    <exclusions>
        <exclusion>
            <groupId>junit</groupId>
            <artifactId>junit-dep</artifactId>
        </exclusion>
    </exclusions>
</dependency>