question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to setup Junit5 testing with dropwizard 2.0.0

See original GitHub issue

Hello, I have been trying to work with extensions to integrate Junit5 test cases into my application. According to the documentation, my understanding is that the extension should be able to spin up a temp server depending on which extension you use. I have tried DropwizardAppExtension, ResourceExtension and DropwizardClientExtension. I am getting a null pointer for the extension in each of the cases and unable to initialize a client to test my resources. Pasting below code for ref.

@ExtendWith(DropwizardExtensionsSupport.class)
public class SessionResourceTest {

//    @ClassRule
//    public static final ResourceTestRule resources = ResourceTestRule.builder()
//            .addResource(new SessionResource((mock(SessionService.class))))
//            .build();

//    @ClassRule
//    public static final DropwizardAppRule<CustomerRepositoryConfiguration> RULE =
//            new DropwizardAppRule<>(CustomerRepositoryApplication.class, ResourceHelpers.resourceFilePath("config-local.yaml"));


//    private static final DropwizardClientExtension dropwizard = new DropwizardClientExtension(new SessionResource(mock(SessionService.class)));

    private static final ResourceExtension RULE = ResourceExtension.builder().addResource(new SessionResource(mock(SessionService.class))).build();

//    public final DropwizardAppExtension<CustomerRepositoryConfiguration> EXTENSION =
//            new DropwizardAppExtension<>(CustomerRepositoryApplication.class, resourceFilePath("config-local.yml"));

//    ResourceExtension resources = ResourceExtension.builder()
//            .addResource(new SessionResource((mock(SessionService.class))))
//            .build();

    private final SessionRequest sessionRequest = new SessionRequest("web", "newtest");

    private static final ObjectMapper MAPPER = Jackson.newObjectMapper();

    @BeforeEach
    public void setup() {
        // Do any mock data prep here.
    }

    @AfterEach
    public void tearDown() {
        // we have to reset the mock after each test
        reset();
    }

    @Test
    public void testCreate() throws Exception {
        final Entity<?> entity = Entity.entity(sessionRequest, MediaType.APPLICATION_JSON_TYPE);

//        assertThat(response.getStatus(), is(200));
//        dropwizard.baseUri();

//        appExtension.getLocalPort();

        final Response response = RULE.target("/session/create").request().post(entity);
        assertThat(response).isInstanceOf(Session.class);

        final Session session = response.readEntity(Session.class);
        assertThat(session.getSessionId()).isNotBlank();
    }
}

I have added my commented code as well, just to show what all I have tried till now. I am new to dropwizard testing and would appreciate any sort of help I can get.

Also, using the earlier Junit4 setup, I was able to get a server up and running but faced issues with dependency injection. After mocking my dependencies, I faced issues with HttpServletRequest as there was no constructor available to create that object.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
feliksikcommented, Sep 30, 2020

I have also just found the solution:

I did

import org.junit.Test;

It is fixed by replacing it with

import org.junit.jupiter.api.Test;

🥳

So this can remain closed, and stay here for reference.

0reactions
zygimantuscommented, Oct 17, 2020

@feliksik , I also had this issue your solution helped me also, thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing Dropwizard
Testing Dropwizard . The dropwizard-testing module provides you with some handy classes for testing your representation classes and resource classes.
Read more >
Junit 5 - Dropwizard-guicey - GitHub Pages
You can use junit 5 extensions with Spock 2. Test environment might be prepared with setup objects and application might be re-configured with...
Read more >
Index (Dropwizard Project 2.0.0-rc5 API) - javadoc.io
after() - Method in class io.dropwizard.testing.junit5. ... An exception thrown to indicate that an Authenticator is unable to check the validity of the ......
Read more >
Maven failure on spring boot test (junit 5) - java - Stack Overflow
Maven surefire plugin config added. I'm using java 8 (it's required for spring boot 2). Memory settings for maven/jvm are default. Just using: ......
Read more >
xvik/dropwizard-guicey - Gitter
from: ru.vyarus.dropwizard.guice.module.installer.feature.jersey.provider. ... Just trying out Dropwizard 2.0.0-rc1 and wondering if guicey 5.0.0-SNAPSHOT ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found