问题
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 artifactId
s, 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.
回答1:
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.
来源:https://stackoverflow.com/questions/17560153/maven-exclusion-not-working