This is very frustrating. I am building an application using dropwizard and testing it with junit5 and the dropwizard-testing module (the junit5 version). Then, I am trying to test a simple endPoint in a resource. The endpoint receives a HttpSession (and the request) but it is always null. I have read a lot but I can not find how to inject the session.
This is my Resource:
@POST
@Path("/doStuff")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_HTML)
public String doStuff(@Session HttpSession session
, @Context HttpServletRequest request) {
// Do the stuff with the session and the request
}
My test is something like this:
@ExtendWith(DropwizardExtensionsSupport.class)
class MyResourceTest {
ResourceExtension resources = ResourceExtension.builder()
.addResource(new MyResource())
.build();
@BeforeEach
void setup() {
}
@Test
void testDoStuff() {
Response response = resources.target("/api/doStuff")
.request(MediaType.APPLICATION_FORM_URLENCODED)
.accept(MediaType.TEXT_HTML)
.post();
System.out.println(response);
}
}
I need to manipulate the session and the request by test. Is it possible? all help is appreciated.
You need to add a test container factory.
ResourceExtension resources = ResourceExtension.builder()
.setTestContainerFactory(new GrizzlyWebTestContainerFactory())
.addResource(new MyResource())
.build();
If you use maven:
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<!-- version>${jersey.version}</version -->
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With