1

I am working on a project that has a dependency of JUnit 4.11 and a transitive dependency of JMock 2.6.0-RC2 who in turn has a dependency of JUnit-dep 4.4. This transitive dependency of JUnit-dep is overriding the setting in my local pom for JUnit. By overriding, I mean that when I call a JUnit method, it calls the one from v4.4 as opposed to v4.11. Adding exclusions for JMock and for JUnit-dep had no effect on my resolved dependencies.

Note: JUnit and JUnit-dep have separate artifactIds, therefore use of one does not omit the other.


Previous Question: JUnit annotation not working

I am attempting to run a test with JUnit's ExpectedException rule yet when I run the test, it appears as though the rule is not running. In the code below, the Exception goes straight through and fails the test as one would expect if the rule were not there.

I recently changed the Maven dependency of my project from 4.3.1 to 4.11. I also did a clean build of my workspace. What steps should I look into to solve this problem? Would trasitive dependencies from Maven confuse the runner? How can I even tell that I am running with 4.11?

@Rule
public ExpectedException thrown= ExpectedException.none();

@Test
public void throwsNullPointerException() {
    thrown.expect(NullPointerException.class);
    throw new NullPointerException();
}

Edit:

I don't know if it helps but adding @RunWith(BlockJUnit4ClassRunner.class) to the top of my class landed me with this:

java.lang.NoSuchFieldError: NULL
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:57)
    at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.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)

EDIT:

Looking at dependencies via m2e in Eclipse: When I check the Dependency Hierarchy tab of my pom, I ser 1 Resolved Dependency for JUnit: 4.11 and multiple (ommitted for conflict with 4.11) versions on the left. In addition there is a Resolved Dependency for junit-dep 4.4 from jmock-junit4: 2.6.0-RC2 however I have marked this dependency as excluded (Right-click + Exclude Maven Artifact...). Further, JMock itself is marked as ommited due to conflict with my own call to JMock 2.1.0.

EDIT: Ctrl+Shift+T RunWith shows 2 versions, 4.4 & 4.11 despite the exclusions.

EDIT: adding a dependency of junit-dep 4.10 "fixed" the problem, finally ommitting the trouble making 4.4 version. Yet this seems more a hack than a solution.

AnthonyW
  • 1,910
  • 5
  • 25
  • 46
  • Are you trying to run the tests in your IDE or with Maven? Are you using a custom JUnit test runner? – TK Gospodinov Jul 09 '13 at 23:55
  • Running in Eclipse. Default runner. – AnthonyW Jul 09 '13 at 23:56
  • In your Run Configuration, check if Eclipse is using the JUnit 3 or JUnit4 runner (Test runner: field in the Test tab). – TK Gospodinov Jul 10 '13 at 00:04
  • @TKGospodinov I am using the JUnit4 runner. Other annotations like `@Test` and `@Before` function, it is the new (post 4.3.1) annotations that are giving me grief. – AnthonyW Jul 10 '13 at 00:08
  • Is there any difference between the JUnit4 runner for 4.3.1 & 4.11? – AnthonyW Jul 10 '13 at 00:10
  • possible duplicate of [NoSuchFieldError when trying to run a jUnit test with Spring](http://stackoverflow.com/questions/7688395/nosuchfielderror-when-trying-to-run-a-junit-test-with-spring) – Matthew Farwell Jul 10 '13 at 06:37
  • For info, the above answer applies to non-spring builds as well. The problem is almost certainly in your build path. – Matthew Farwell Jul 10 '13 at 06:38
  • 1
    One possibility is that you have multiple JUnit dependencies in your classpath. Could you please execute `mvn dependency:tree` and see what versions of JUnit are listed? Specifically this answer provides more details about the NoSuchFieldError: http://stackoverflow.com/a/7689057/1570834 – DB5 Jul 10 '13 at 06:39

1 Answers1

0

It looks like you still have JUnit 4.3 (or at least pre 4.5) somewhere on your build path. See NoSuchFieldError when trying to run a jUnit test with Spring for more details.

Community
  • 1
  • 1
Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171