shahshi15
shahshi15

Reputation: 2987

DropwizardServiceRule throwing NullPointerException after tests are finished running

I just cannot figure out why DropwizardServiceRule suddenly started throwing errors on test completions. Here's the error:

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)

And here's how I initialize the rule:

@ClassRule
public static final DropwizardServiceRule<RestServiceConfiguration> RULE = TestHelper.getDropwizardStartUpRule();

And my TestHelper looks like this:

public static DropwizardServiceRule<RestServiceConfiguration> getDropwizardStartUpRule()
    {
        return new DropwizardServiceRule<RestServiceConfiguration>(RestService.class,
                Resources.getResource("rest-test.yml").getPath());
    }

I checked the DropwizardServiceRule code and seems like the NullPointer is thrown from here:

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

All help appreciated !

Upvotes: 1

Views: 1080

Answers (1)

Dan1701
Dan1701

Reputation: 487

This error can be misleading, often the root cause is a problem encountered when setting up the ServiceRule, such as:

  • The yml file couldn't be located or wasn't parsed properly.
  • One of the classes used as part of the service Configuration looks for a separate config file (Shiro .ini, for example), and that file couldn't be found or read.

Check for the presence of any of these problems first before dealing with the specific NPE shown in the stack.

Upvotes: 1

Related Questions