If what your filter does is not too complex, you could subclass com.sun.jersey.spi.container.servlet.ServletContainer, and do the filter logic in the service() method.
This is feasible for me because I only have one filter that does some authentication, and in the test environment I simply set an attribute on the HttpServletRequest to simulate what the filter normally does.
public static class TestServletContainer extends ServletContainer {
private static final long serialVersionUID = 1L;
@Override
public void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
doFilterStuffHere();
super.service(request, response);
}
}
And I have an AbstractTest method that extends JerseyTest:
public abstract class AbstractTest extends JerseyTest {
protected AbstractTest() throws Exception {
super(new WebAppDescriptor.Builder("somepackage").servletClass(
TestServletContainer.class).build());
}
...
It's a little clunky, but it works for my needs.
I'm not on the Jersey development team, so don't consider this an official answer. :)
Charles
----- Original Message ----
From: schong <dr.steve.chong@gmail.com>
To: users@jersey.dev.java.net
Sent: Tue, April 27, 2010 2:25:42 PM
Subject: [Jersey] Filter not executing when running a JerseyTest
Hello,
I’m having difficulties trying to execute a Filter using the Jersey Test
Framework.
We’ve got a Maven Project called Customer Registration. This has two
modules. A REST module (produces a JAR) and a Web module (produces WAR) .
The Web module has a dependency on the REST JAR.
In the Web module we’ve got a Filter that is mapped to the Jersey Servlet
(in the web.xml) so that any requests have to the Filter first.
What I am trying to do is to run tests using a JerseyTest class against the
Web module running on a GrizzlyWebServer. The theory is that any requests to
our REST service will go to the Filter.
My problem is that the GrizzlyWebServer starts up fine and the tests run
against it but the Filter is not executed.
I ran separate JUnit tests (Apache HttpClient) against a Jetty instance with
the same web module (using mvn jetty:run from the command line). The Filter
is executed fine.
I’d really like to have the tests running as part of my JerseyTest so that
we can run the tests as part of our build process.
Does anyone have any pointers or suggestions?
Thanks, Steve.
--
View this message in context:
http://jersey.576304.n2.nabble.com/Filter-not-executing-when-running-a-JerseyTest-tp4971178p4971178.html
Sent from the Jersey mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@jersey.dev.java.net
For additional commands, e-mail: users-help@jersey.dev.java.net