2

我在 Java 中使用 jUnit 测试和处理异常时遇到问题。我使用注释@RunWith (DevAppServerTestRunner.class) 和@DevAppServerTest (TestConfig.class) 创建了我的 TestClass,因为我需要测试一个使用我的 appengine-web.xml 文件中配置的一些属性的方法。

问题来了,在声明了一个 jUnit @Rule来定义一个我想测试的异常之后,我运行了测试并得到一个错误说“规则' myexceptionName '必须实现 MethodRule 或 TestRule”。

这很奇怪,因为我声明的异常是ExpectedException类型,这是一个已经实现 TestRule 的 jUnit 类。我怀疑这个问题与我的@RunWith@DevAppServerTest注释有关,因为使用另一个没有它们的类,规则实现工作(我不能使用的解决方案,因为我需要从 appengine-web.xml 读取一些属性)。

有没有人遇到过类似的事情或知道如何处理这个问题?


@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();  
        }
    }   

我在这里编写的示例代码不涉及 appengine 系统属性,但它给出同样的错误:

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)
4

0 回答 0