HealthCheckServlet extends HttpServlet { @Override public void init(ServletConfig config) throws ServletException { super.init(config); final ServletContext context = config.getServletContext(); if (null == registry) { final Object registryAttr = context.getAttribute(HEALTH_CHECK_REGISTRY); if (registryAttr instanceof HealthCheckRegistry) { this.registry = (HealthCheckRegistry) registryAttr; } else { throw new ServletException("Couldn't find a HealthCheckRegistry instance."); } } final Object executorAttr = context.getAttribute(HEALTH_CHECK_EXECUTOR); if (executorAttr instanceof ExecutorService) { this.executorService = (ExecutorService) executorAttr; } final Object filterAttr = context.getAttribute(HEALTH_CHECK_FILTER); if (filterAttr instanceof HealthCheckFilter) { filter = (HealthCheckFilter) filterAttr; } if (filter == null) { filter = HealthCheckFilter.ALL; } this.mapper = new ObjectMapper().registerModule(new HealthCheckModule()); } HealthCheckServlet();  HealthCheckServlet(HealthCheckRegistry registry); @Override void init(ServletConfig config); @Override void destroy(); static final String HEALTH_CHECK_REGISTRY; static final String HEALTH_CHECK_EXECUTOR; static final String HEALTH_CHECK_FILTER; }
@Test public void constructorWithRegistryAsArgumentIsUsedInPreferenceOverServletConfig() throws Exception { final HealthCheckRegistry healthCheckRegistry = mock(HealthCheckRegistry.class); final ServletContext servletContext = mock(ServletContext.class); final ServletConfig servletConfig = mock(ServletConfig.class); when(servletConfig.getServletContext()).thenReturn(servletContext); final HealthCheckServlet healthCheckServlet = new HealthCheckServlet(healthCheckRegistry); healthCheckServlet.init(servletConfig); verify(servletConfig, times(1)).getServletContext(); verify(servletContext, never()).getAttribute(eq(HealthCheckServlet.HEALTH_CHECK_REGISTRY)); }
@Test public void constructorWithRegistryAsArgumentUsesServletConfigWhenNull() throws Exception { final HealthCheckRegistry healthCheckRegistry = mock(HealthCheckRegistry.class); final ServletContext servletContext = mock(ServletContext.class); final ServletConfig servletConfig = mock(ServletConfig.class); when(servletConfig.getServletContext()).thenReturn(servletContext); when(servletContext.getAttribute(eq(HealthCheckServlet.HEALTH_CHECK_REGISTRY))) .thenReturn(healthCheckRegistry); final HealthCheckServlet healthCheckServlet = new HealthCheckServlet(null); healthCheckServlet.init(servletConfig); verify(servletConfig, times(1)).getServletContext(); verify(servletContext, times(1)).getAttribute(eq(HealthCheckServlet.HEALTH_CHECK_REGISTRY)); }
@Test(expected = ServletException.class) public void constructorWithRegistryAsArgumentUsesServletConfigWhenNullButWrongTypeInContext() throws Exception { final ServletContext servletContext = mock(ServletContext.class); final ServletConfig servletConfig = mock(ServletConfig.class); when(servletConfig.getServletContext()).thenReturn(servletContext); when(servletContext.getAttribute(eq(HealthCheckServlet.HEALTH_CHECK_REGISTRY))) .thenReturn("IRELLEVANT_STRING"); final HealthCheckServlet healthCheckServlet = new HealthCheckServlet(null); healthCheckServlet.init(servletConfig); }