Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NullPointerException at io.dropwizard.testing.junit5.DropwizardExtensionsSupport.beforeEach

I am trying to test a Dropwizard resource.

My test looks like this:

@ExtendWith(DropwizardExtensionsSupport.class)
public class CommonObjectsTest {
    private ResourceExtension EXT;

    @BeforeEach
    public void setup() {
        ApplicationConfig applicationConfig = mock(ApplicationConfig.class);
        when(applicationConfig.getYears()).thenReturn(1);

        MyAppConfig myAppConfig = mock(MyAppConfig.class);
        when(myAppConfig.getAppConfig()).thenReturn(applicationConfig);

        EmailClient emailClient = mock(EmailClient.class);
        CommonObjects commonObjects = new CommonObjects(myAppConfig, emailClient);

        EXT = ResourceExtension.builder()
                .addResource(commonObjects)
                .build();
    }

    @Test
    public void getYearsSuccessfully() {
        Response response = EXT.target("/get_years").request().get();
        System.out.println(response);
    }
}

However, this gives the error message:

java.lang.NullPointerException
    at io.dropwizard.testing.junit5.DropwizardExtensionsSupport.beforeEach(DropwizardExtensionsSupport.java:123)
    at io.dropwizard.testing.junit5.DropwizardExtensionsSupport.beforeEach(DropwizardExtensionsSupport.java:106)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeEachCallbacks$1(TestMethodTestDescriptor.java:159)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeMethodsOrCallbacksUntilExceptionOccurs$5(TestMethodTestDescriptor.java:195)
    ...

which is frankly uninformative. Can someone point out what is wrong here?

P/S Here is the CommonObjects constructor:

    public CommonObjects(MyAppConfig configuration, EmailClient emailClient) {
        ApplicationConfig appConfig = configuration.getAppConfig();
        this.years = appConfig.getYears();
        this.emailClient = emailClient;
    }

which also explains why I am creating the resource extension before each test case.

like image 884
umop apisdn Avatar asked Jan 27 '26 19:01

umop apisdn


1 Answers

This may not be the solution to this answer, however I had a similar exception with the bonehead problem of running the extension in vintage JUnit rather than Jupiter which is required for the ExtendWith annotation. Maybe this will save someone an hour or two.

NullPointerException:

import org.junit.Test;

Works:

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
like image 74
Phillip Fleischer Avatar answered Jan 29 '26 11:01

Phillip Fleischer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!