简体   繁体   中英

How to inject a HttpSession and HttpServletRequest when unit-testing dropwizard in junit5?

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>

I had a similiar problem and solved it with a test container factory too. However my approach is different from the one jgl posted earlier.

public class SessionRestTest extends JerseyTest {

    @Override
    protected TestContainerFactory getTestContainerFactory() {
        // standard servername and port is localhost:9998
        return new GrizzlyWebTestContainerFactory();
    }

    @Override
    protected DeploymentContext configureDeployment() {
        ResourceConfig config = new ResourceConfig(SessionRest.class);
        return ServletDeploymentContext.forServlet(
                new ServletContainer(config)).build();
    }

    private Response get() {
        return target("/session/logout").request().get();
    }

    @Test
    public void testRedirectURL() {
        Response response = get();
        assertEquals("should return status 200", HttpStatus.OK_200, response.getStatus());
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM