Reputation: 674
My DropWizard JAX-RS project was using the DW's now-deprecated JUnit 4 rule for testing, which at least worked. I recently attempted to change to JUnit 5 and use the mainstream method for testing and I cannot get the tests to even start. DW gives a classloading exception loading my test but buried deep in the caused-by there is a line "Caused by: java.lang.NullPointerException: environment". I've trimmed out most of the other lines for brevity:
java.lang.Exception: java.lang.ExceptionInInitializerError
at io.dropwizard.testing.junit5.DropwizardExtensionsSupport.beforeAll(DropwizardExtensionsSupport.java:87)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeBeforeAllCallbacks$8(ClassBasedTestDescriptor.java:368)
...
Suppressed: java.lang.Exception: java.lang.NoClassDefFoundError: Could not initialize class com.idfconnect.myapp.ws.test.MyAppServiceTest
...
Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.idfconnect.myapp.ws.test.MyAppServiceTest
...
Caused by: java.lang.NullPointerException: environment
at java.util.Objects.requireNonNull(Objects.java:228)
at io.dropwizard.testing.DropwizardTestSupport.getEnvironment(DropwizardTestSupport.java:371)
at io.dropwizard.testing.DropwizardTestSupport.getObjectMapper(DropwizardTestSupport.java:375)
at io.dropwizard.testing.junit5.DropwizardAppExtension.getObjectMapper(DropwizardAppExtension.java:242)
at io.dropwizard.testing.junit5.DropwizardAppExtension.clientBuilder(DropwizardAppExtension.java:279)
at io.dropwizard.testing.junit5.DropwizardAppExtension.client(DropwizardAppExtension.java:271)
at com.idfconnect.myapp.ws.test.MyAppServiceTest.<clinit>(MyAppServiceTest.java:38)
... 46 more
I'm sort of at a loss as to why it is failing. Clearly it is loading the class and attempting to initialize it and the call to getEnvironment in DropwizardTestSupport is returning null.
Here is the top of the test class:
@ExtendWith(DropwizardExtensionsSupport.class)
public class MyAppServiceTest {
static Logger logger = LoggerFactory.getLogger(MyAppServiceTest.class);
static final String BASE_URI = "http://localhost:8080/myapp";
private static DropwizardAppExtension<MyAppConfiguration> mkApp = new DropwizardAppExtension<>(
MyAppApplication.class,
ResourceHelpers.resourceFilePath("my-app-config.yaml"));
private static Client client = mkApp.client();
Any advice appreciated!
Only other thing I'll note is that the app itself runs fine
Upvotes: 0
Views: 1736
Reputation: 3065
Can also happen if your @Test annotation was imported from org.junit
/Junit4 and not org.junit.jupiter.api.Test
/Junit5
Upvotes: 4
Reputation: 674
In case anyone else encounters the same issue, it was caused solely by making "Client" static.
private static Client client = mkApp.client();
As soon as I made client an instance variable, it worked fine. Go figure...
Upvotes: 0