猿问

在junit5中单元测试dropwizard时如何注入HttpSession

这是非常令人沮丧的。我使用dropwizard建立一个应用程序,并测试它junit5和dropwizard-测试模块(在junit5版)。然后,我试图在资源中测试一个简单的端点。端点接收 HttpSession(和请求),但它始终是null。我读了很多,但我找不到如何注入会话。


这是我的资源:


@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

}

我的测试是这样的:


@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);

    }

}

我需要通过测试来操作会话和请求。是否可以?感谢所有帮助。


临摹微笑
浏览 258回答 2
2回答

呼啦一阵风

您需要添加一个测试容器工厂。ResourceExtension resources = ResourceExtension.builder()&nbsp; &nbsp; .setTestContainerFactory(new GrizzlyWebTestContainerFactory())&nbsp; &nbsp; .addResource(new MyResource())&nbsp; &nbsp; .build();如果您使用 Maven:<dependency>&nbsp; &nbsp; <groupId>org.glassfish.jersey.test-framework.providers</groupId>&nbsp; &nbsp; <artifactId>jersey-test-framework-provider-grizzly2</artifactId>&nbsp; &nbsp; <!-- version>${jersey.version}</version -->&nbsp; &nbsp; <scope>test</scope>&nbsp; &nbsp; <exclusions>&nbsp; &nbsp; &nbsp; &nbsp; <exclusion>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>javax.servlet</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>javax.servlet-api</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </exclusion>&nbsp; &nbsp; &nbsp; &nbsp; <exclusion>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>junit</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>junit</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </exclusion>&nbsp; &nbsp; </exclusions></dependency>

HUWWW

我有一个类似的问题,也用一个测试容器工厂解决了它。但是,我的方法与 jgl 之前发布的方法不同。public class SessionRestTest extends JerseyTest {&nbsp; &nbsp; @Override&nbsp; &nbsp; protected TestContainerFactory getTestContainerFactory() {&nbsp; &nbsp; &nbsp; &nbsp; // standard servername and port is localhost:9998&nbsp; &nbsp; &nbsp; &nbsp; return new GrizzlyWebTestContainerFactory();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected DeploymentContext configureDeployment() {&nbsp; &nbsp; &nbsp; &nbsp; ResourceConfig config = new ResourceConfig(SessionRest.class);&nbsp; &nbsp; &nbsp; &nbsp; return ServletDeploymentContext.forServlet(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ServletContainer(config)).build();&nbsp; &nbsp; }&nbsp; &nbsp; private Response get() {&nbsp; &nbsp; &nbsp; &nbsp; return target("/session/logout").request().get();&nbsp; &nbsp; }&nbsp; &nbsp; @Test&nbsp; &nbsp; public void testRedirectURL() {&nbsp; &nbsp; &nbsp; &nbsp; Response response = get();&nbsp; &nbsp; &nbsp; &nbsp; assertEquals("should return status 200", HttpStatus.OK_200, response.getStatus());&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答