Guiced servlets provide a means to access a root servlet listener, with everything you could possibly need already configured - such as a Filters and context bindings,
and then allows you to bind with the Servlet Module that performed such actions
<dependency> <groupId>com.guicedee.servlets</groupId> <artifactId>guiced-servlets</artifactId> </dependency>
provides IGuiceSiteBinder with MySiteBinder
This allows submodules in the Java Modular System to bind safely into a singular point, and enables handling of registered filters in a root servlet module.
The module also provides a set of keys, that can be used in place of
HttpServletRequest
or the likes, for overriding the injections.
You are required to annotate your Servlets as
@Singletons
as usual with Guice.
@Singleton public class BasicServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); } }Then we create a binder, to bind the servlet, and modify it accordingly
public class ServletTestBindings implements IGuiceSiteBinder<GuiceSiteInjectorModule> { @Override public void onBind(GuiceSiteInjectorModule module) { module.serveRegex$("/sitetest*") .with(BasicServlet.class); } }
Servlet Testing
You can implement servlet testing any way you like, but Guiced Servlets provides a couple of useful things in the test dependency<dependency> <groupId>com.guicedee.services</groupId> <artifactId>guice-servlet</artifactId> <type>test-jar</type> <scope>test</scope> </dependency>Including this dependency gives access to keys, and mocked servlet object types, with their bindings, that you can use and configure accordingly.
The Guiced Servlets test module binds the following
@Override public void onBind(GuiceSiteInjectorModule module) { module.bind(GuicedServletKeys.getHttpSessionKey()) .to(MockHTTPSession.class); module.bind(GuicedServletKeys.getServletRequestKey()) .to(MockRequest.class); module.bind(GuicedServletKeys.getHttpServletRequestKey()) .to(MockRequest.class); module.bind(GuicedServletKeys.getServletResponseKey()) .to(MockResponse.class); module.bind(GuicedServletKeys.getHttpServletResponseKey()) .to(MockResponse.class); module.bind(GuicedServletKeys.getServletContextKey()) .to(MockServletContext.class); }This library then sets the override keys to these instances for usage anywhere in your tests.
These allow injections to these objects when accessing Servlet objects
public class GuiceKeysOverride implements IGuicePreStartup<GuiceKeysOverride> { @Override public void onStartup() { GuicedServletKeys.setHttpServletRequestKey(Key.get(HttpServletRequest.class, Names.named("TEST"))); GuicedServletKeys.setHttpServletResponseKey(Key.get(HttpServletResponse.class, Names.named("TEST"))); GuicedServletKeys.setHttpSessionKey(Key.get(HttpSession.class, Names.named("TEST"))); GuicedServletKeys.setServletContextKey(Key.get(ServletContext.class, Names.named("TEST"))); GuicedServletKeys.setServletRequestKey(Key.get(ServletRequest.class, Names.named("TEST"))); GuicedServletKeys.setServletResponseKey(Key.get(ServletResponse.class, Names.named("TEST"))); } @Override public Integer sortOrder() { return 101; } }
By doing all of this for you, a test may look like the following for a
@RequestScoped
or @SessionScoped
instance.
@Test void testFakeRequestScope() { GuiceContext.inject(); ServletScopes.scopeRequest(new HashMap()) .open(); RequestScopedObject obj = GuiceContext.get(RequestScopedObject.class); BasicServlet servlet = GuiceContext.get(BasicServlet.class); HttpServletResponse resp = servlet.get(GuicedServletKeys.getHttpServletRequestKey(), GuicedServletKeys.getHttpServletResponseKey()); }