1

I have one unit test case which uses the annotation @RunWith(SpringJUnit4ClassRunner.class).

Everything works fine with spring 2.5.6 and with @RunWith(SpringJUnit4ClassRunner.class) annotation. But when i shift spring version to 3.2.2-Release and if @RunWith(SpringJUnit4ClassRunner.class) is used it throws the following exception

java.lang.NoSuchFieldError: NULL
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:48)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:58)
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:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
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:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
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)

But with spring 3.2.2-Release and when @RunWith(SpringJUnit4ClassRunner.class) is NOT used it just works fine.

I have the following dependency in my pom

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>

Dont know what is going wrong!!

user2649233
  • 912
  • 4
  • 14
  • 28
  • this seems to describe your issue: http://stackoverflow.com/questions/7688395/nosuchfielderror-when-trying-to-run-a-junit-test-with-spring. From that question it looks like you are having an issue with different JUnit versions. – DB5 Nov 11 '13 at 13:09
  • I'm looking at the sources of ParentRunner for JUnit 4.11, and at line 48 where the error is indicated in your stacktrace there's a javadoc. I can see that you're running from Eclipse so I suspect you might have another library shadowing the maven dependency, because I'm using spring 3.2.2 & junit 4.11 without any problem. Check the project's classpath – Morfic Nov 11 '13 at 13:10
  • 2
    First, put `test` in your pom for JUnit. Then, I had similar problems with JUnit and Hamcrest, and I solved ensuring that the definition of Hamcrest was BEFORE the one of JUnit. So, try defining JUnit BEFORE Spring in your pom. Otherwise, we may need to see the output of `mvn dependency:tree` to see if your imports show any problem. – ThanksForAllTheFish Nov 11 '13 at 13:23
  • 1
    @mardavi By defining JUNIT dependency before SPRING dependency it started working for me :) – user2649233 Nov 11 '13 at 13:42
  • My guess mixing versions of spring jars on your classpath. This is a known issue when using a newer spring version but having an older jar lingering somewhere. – M. Deinum Nov 11 '13 at 14:13
  • Insanity. Moving JUnit to the top of the dependencies worked for me too, even though I had all the same versions of Spring and only a single version of JUnit. WTF?!?!?!?!?! Why? Thanks, though, for the tip. This cost me many hours. – Janx Dec 04 '13 at 01:39

1 Answers1

0

This is most likely due to importing conflicting versions of JUnit from your dependencies. I had the same problem and I found out it was caused by a transitive dependency with org.jmock:jmock-junit4. It was fixed when I replaced it with org.jmock:jmock.

Micho
  • 3,929
  • 13
  • 37
  • 40