jersey2 Unit testing,HttpServletRequest is null

后端 未结 1 1680
庸人自扰
庸人自扰 2020-12-11 21:25

Ask everybody to help?

jersey Bug connection: [1]: https://java.net/jira/browse/JERSEY-2412

The servlet request, response and context not injected

相关标签:
1条回答
  • 2020-12-11 21:54

    You need to configure the JerseyTest for a Servlet environment. In your JerseyTest, you should have something like

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

    If you look at the ServletDeploymentContext.forServlet, it returns a ServletDeploymentContext.Builder. If you look at the Javadoc, you will see some familiar looking methods, like initParam(...,...), addListener, etc. This is just like building your web.xml programmatically. Just keep chaining methods, then build.

    With the above configuration, you no longer need to override the configure method in the JerseyTest. Just add the ResourceConfig like seen above.

    See other test examples here

    Also See related:

    • How to in-memory unit test Spring-Jersey
    0 讨论(0)
提交回复
热议问题