Unable to setup Junit5 testing with dropwizard 2.0.0
See original GitHub issueHello, 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:
- Created 3 years ago
- Comments:12 (4 by maintainers)
I have also just found the solution:
I did
It is fixed by replacing it with
🥳
So this can remain closed, and stay here for reference.
@feliksik , I also had this issue your solution helped me also, thank you.