IsActiveServlet extends HttpServlet { @Override public void doGet( final HttpServletRequest req, final HttpServletResponse resp) throws IOException { resp.addHeader("Connection", "close"); if (!isActive()) { resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, RESPONSE_NOT_ACTIVE); return; } resp.setStatus(HttpServletResponse.SC_OK); resp.getWriter().write(RESPONSE_ACTIVE); resp.getWriter().flush(); }  @Override void doGet(
      final HttpServletRequest req, final HttpServletResponse resp); static final String SERVLET_NAME; static final String PATH_SPEC; static final String RESPONSE_ACTIVE; static final String RESPONSE_NOT_ACTIVE; }
@Test public void testSucceedsOnActive() throws IOException { servlet = new IsActiveServlet() { @Override protected boolean isActive() { return true; } }; String response = doGet(); verify(resp, never()).sendError(anyInt(), anyString()); assertEquals(IsActiveServlet.RESPONSE_ACTIVE, response); }
@Test public void testFailsOnInactive() throws IOException { servlet = new IsActiveServlet() { @Override protected boolean isActive() { return false; } }; doGet(); verify(resp, atLeastOnce()).sendError( eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), eq(IsActiveServlet.RESPONSE_NOT_ACTIVE)); }