Ask everybody to help?
jersey Bug connection: [1]: https://java.net/jira/browse/JERSEY-2412
The servlet request, response and context not injected
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: