0

Problem

I am working on a RESTful API using Jersey. To allow mocking of objects in my unit tests, I use the dependency injection framework Guice (in particular Guice-Servlet). It is possible to filter requests through your own Filter implementations, by overriding the configureServlet() method of your ServletModule and adding:

bind(MyFilter.class).in(Singleton.class);
filter("path").through(MyFilter.class);

When running my webapp in Tomcat, my requests are correctly filtered through this MyFilter class. But, in my JUnit tests none of my requests are filtered through it. Can anybody help me find the problem?

Example

Bitbucket repository with my example code

  • web / WEB-INF / web.xml : used by Tomcat. Defines ApplicationSetup as the GuiceFilter on path '/api/*'.
  • src / main / java / ApplicationSetup.java : creates the Guice Injector for the production environment (Tomcat webserver). Uses ApplicationModule as one of its ServletModules.
  • src / main / java / ApplicationModule.java : ServletModule that configures and binds MyFilter and the Jersey endpoints on path '/api/*'.
  • src / main / java / endpoints / HelloWorldEndPoint.java : Simple endpoint at path '/api/helloworld' with method for GET request.
  • src / main / java / MyFilter.java : Sets the 'Via' header on the response if the request passed through this filter.
  • src / test / java / TestApplicationSetup.java : creates the Guice Injector for the test environment (Grizzly webserver). Like ApplicationSetup this also uses ApplicationModule as one of its ServletModules. In addition, it has a method to start the Grizzly webserver for test purposes.
  • src / test / java / TestHelloWorldEndPoint.java : Performs a GET request on /api/helloworld and checks if the response has the 'Via' header set to MyFilter. This fails.

The main point here is the ApplicationModule, which is used by both environments to bind the filter. If you check the logging of the example you'll notice that MyFilter is initialised in both environments (simple print in the init() method). You'll also notice that the ApplicationModule is used for the servlet configuration in both environments. But somehow the Grizzly webserver ignores the Filter.

I've been struggling with this problem for quite some time now, and thus have now taken the time to ask this question here. I really hope somebody can help me with this. Workarounds are also appreciated.

Korthout
  • 371
  • 3
  • 12
  • 1
    Try using Jersey Test Framework, as [mentioned here](http://stackoverflow.com/a/29359524/2587435). – Paul Samsotha May 30 '15 at 00:08
  • I think you have to use Grizzly servlets module, similar to http://stackoverflow.com/questions/18080911/how-to-use-jersey-2-0-guice-on-grizzly – alexey Jun 01 '15 at 15:15
  • @peeskillet thank you, I will definitely try this first. It seems like the right thing to do. – Korthout Jun 02 '15 at 12:35
  • @alexey I do not believe this will work. Guice's depedency injection (using GuiceFilter) is working correctly. The problem here is purely filtering the requests using ServletModule's _filter through_ method. But, if I find no other solutions I will try this. – Korthout Jun 02 '15 at 12:36
  • Look at the Jersey 1 example I linked to in the post, for the Maven dependency. But @alexey is right though. The Guice module needs to run in a servlet environment. Looking at your code, you currently don't have Grizzly set up that way. I only suggested Jersey Test Framework because it's easier (no work really) to run Grizzly in a servlet container. It's pretty much set up that way. – Paul Samsotha Jun 02 '15 at 12:48
  • Thank you both again for your input. After spending several days of reading and trying to get it all to work, I decided to drop Guice as the dependency injection framework. I just couldn't get the Filter to work. Instead, I took peeskillet's suggestion to use Jersey Test Framework 2, which supports [dependency injection](https://jersey.java.net/documentation/latest/ioc.html) using the [HK2](https://hk2.java.net/) library. A new branch in the Bitbucket repo now contains a working version of the example application using this framework. Although I can continue, the original problem still exists. – Korthout Jun 24 '15 at 16:37

0 Answers0