Requests on Jersey endpoints are not filtered through Filter by Guice's ServletModule in my JUnit test -
problem
i working on restful api using jersey. allow mocking of objects in unit tests, use dependency injection framework guice (in particular guice-servlet). possible filter requests through own filter implementations, overriding configureservlet() method of servletmodule , adding:
bind(myfilter.class).in(singleton.class); filter("path").through(myfilter.class);
when running webapp in tomcat, requests correctly filtered through myfilter class. but, in junit tests none of requests filtered through it. can me find problem?
example
bitbucket repository example code
- web / web-inf / web.xml : used tomcat. defines applicationsetup guicefilter on path '/api/*'.
- src / main / java / applicationsetup.java : creates guice injector production environment (tomcat webserver). uses applicationmodule 1 of servletmodules.
- src / main / java / applicationmodule.java : servletmodule configures , binds myfilter , jersey endpoints on path '/api/*'.
- src / main / java / endpoints / helloworldendpoint.java : simple endpoint @ path '/api/helloworld' method request.
- src / main / java / myfilter.java : sets 'via' header on response if request passed through filter.
- src / test / java / testapplicationsetup.java : creates guice injector test environment (grizzly webserver). applicationsetup uses applicationmodule 1 of servletmodules. in addition, has method start grizzly webserver test purposes.
- src / test / java / testhelloworldendpoint.java : performs request on /api/helloworld , checks if response has 'via' header set myfilter. fails.
the main point here applicationmodule, used both environments bind filter. if check logging of example you'll notice myfilter initialised in both environments (simple print in init() method). you'll notice applicationmodule used servlet configuration in both environments. somehow grizzly webserver ignores filter.
i've been struggling problem quite time now, , have taken time ask question here. hope can me this. workarounds appreciated.
Comments
Post a Comment