Filters in JerseyTest 2.x

I am writing a test using JerseyTest v2.5 and the Grizzly container. My resource depends on the filter defined in my web.xml, so the test does not work.

Is there a way to configure a servlet filter when using JerseyTest?

Even better, is there a way to use my web.xml to configure the servlet container?

+6
source share
3 answers

There a problem about it has already opened, which you could see: https://java.net/jira/browse/JERSEY-2259

Ultimately it seems

you cannot register the servlet and filter classes at the same time as the knitwear testing framework (the installation will erase another one).

This quote is taken from this answer by Pavel Buchek .

I wonder if you saw this, it seems to have found a workaround, but I'm not sure if this applies to you.

What you can do is deploy the application on (for example) the built-in glass fish and conduct tests against it using external container support. The command to run the tests will look like this:

T-shirt version 1.2+:

mvn test

  • Djersey.test.containerFactory = com.sun.jersey.test.framework.spi.container.external.ExternalTestContainerFactory

  • Djersey.test.port = XXX -Djersey.test.host = XXX

Jersey version 1.1.5.1-:

mvn test

  • Djersey.test.containerFactory = com.sun.jersey.test.framework.spi.container.external.ExternalTestContainerFactory

  • DJERSEY_HTTP_PORT = XXX -DJERSEY_HOST_NAME = XXX

+9
source

After exploring this, it seems impossible to combine servlet filters with JerseyTest - everything that needs to be applied should be implemented as Jersey / JAX -RS filters.

To test in conjunction with servlet filters, a WAR must be deployed to a servlet container (inline or otherwise).

+2
source

I have not tested it, but maybe this will help you.

Filter availability

public class MyFilter implements ContainerRequestFilter { @Context HttpServletRequest webRequest; @Override public void filter(ContainerRequestContext requestContext) throws IOException { final HttpSession session = webRequest.getSession(); ...... } } 

You can define your resource configuration class and then register your filter

 import org.glassfish.jersey.server.ResourceConfig; import javax.ws.rs.ApplicationPath; @ApplicationPath("/rest") public class MyResourceConfig extends ResourceConfig { // Initializes all resources from your REST package. public MyResourceConfig() { // add your package name to enable package scanning for resources. packages("com.myapp.rest"); // or you can register your resource class directly register(YourResource.class); register(MyFilter.class); } } 

then in the jersey test class

 @Override protected Application configure() { return new MyResourceConfig (); } 

or in a simpler way

 @Override protected Application configure() { return new ResourceConfig(YourResource.class).register(MyFilter.class); } 

if you need to set a property for your filter, you can do

 @Override protected Application configure() { return new MyResourceConfig ().property("property", "value"); } 

or

 @Override protected Application configure() { return new ResourceConfig(YourResource.class).register(MyFilter.class).property("property", "value"); } 
+1
source

All Articles