我无法弄清楚为什么DropwizardServiceRule突然开始在测试完成时抛出错误。这是错误:
java.lang.NullPointerException
at com.yammer.dropwizard.testing.junit.DropwizardServiceRule$1.evaluate(DropwizardServiceRule.java:40)
at org.junit.rules.RunRules.evaluate(RunRules.java:18)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
以下是我如何初始化规则:
@ClassRule
public static final DropwizardServiceRule<RestServiceConfiguration> RULE = TestHelper.getDropwizardStartUpRule();
我的TestHelper看起来像这样:
public static DropwizardServiceRule<RestServiceConfiguration> getDropwizardStartUpRule()
{
return new DropwizardServiceRule<RestServiceConfiguration>(RestService.class,
Resources.getResource("rest-test.yml").getPath());
}
我检查了DropwizardServiceRule代码,似乎从这里抛出了NullPointer:
@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
startIfRequired();
try {
base.evaluate();
} finally {
jettyServer.stop();
}
}
};
所有帮助表示赞赏!
答案 0 :(得分:1)
此错误可能会产生误导,通常根本原因是在设置ServiceRule时遇到的问题,例如:
在处理堆栈中显示的特定NPE之前,首先检查是否存在任何这些问题。