Reputation: 21
I am having an issue using jUnit tests and handling exceptions in Java. I created my TestClass with both the annotations @RunWith(DevAppServerTestRunner.class) and @DevAppServerTest(TestConfig.class) since I need to test a method that use some properties configured in my appengine-web.xml file.
The problem comes when, after declaring a jUnit @Rule to define an Exception I want to test, I ran the test and got an error saying "The Rule 'myexceptionName' must implement MethodRule or TestRule".
That's pretty strange since the exception I declared is of type ExpectedException, a jUnit class that already implements TestRule. I suspect this issue has something to do with my @RunWith and @DevAppServerTest annotations because using another class without them, the Rule implementation works(solution I can't use since I need to read some properties from appengine-web.xml).
Did anyone encounter something similar or have a clue on how to deal with this?
@RunWith(DevAppServerTestRunner.class)
@DevAppServerTest(TestConfig.class)
public class myTestClass {
@Rule
public ExpectedException exc = ExpectedException.none();
@Test
public void TestingException() throws Exception {
this.exc.expect(NullPointerException.class);
String test = null;
test.length();
}
}
The example code I wrote here doesn't involve appengine system properties, but it gives this same error:
org.junit.internal.runners.rules.ValidationError: The @Rule 'exc' must implement MethodRule or TestRule.
at org.junit.internal.runners.rules.RuleMemberValidator$FieldMustBeARule.validate(RuleMemberValidator.java:234)
at org.junit.internal.runners.rules.RuleMemberValidator.validateMember(RuleMemberValidator.java:99)
at org.junit.internal.runners.rules.RuleMemberValidator.validate(RuleMemberValidator.java:93)
at org.junit.runners.BlockJUnit4ClassRunner.validateFields(BlockJUnit4ClassRunner.java:196)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:129)
at com.google.appengine.tools.development.testing.DevAppServerTestRunner.collectInitializationErrors(DevAppServerTestRunner.java:102)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
at com.google.appengine.tools.development.testing.DevAppServerTestRunner.<init>(DevAppServerTestRunner.java:74)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:90)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:76)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:49)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:525)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Upvotes: 2
Views: 105