MaintenanceStatusServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { String result = contentProvider.getContent(); resp.setContentType("application/json"); resp.getWriter().write(result); } catch (IOException e) { resp.setStatus(500); resp.setContentType("application/json"); resp.getWriter().write("{\"error\":\"" + e.getMessage() + "\"}"); } } @Inject  MaintenanceStatusServlet(StatusPageContentProvider contentProvider);   }
@Test public void shouldWriteContentToResponse() throws ServletException, IOException { when(contentProvider.getContent()).thenReturn("JSON"); when(resp.getWriter()).thenReturn(responseWriter); servlet.doGet(req, resp); verify(resp).getWriter(); verify(responseWriter).write(eq("JSON")); }
@Test public void shouldWriteErrorToResponceWrappedInJson() throws ServletException, IOException { doThrow(new IOException("error message")).when(contentProvider).getContent(); when(resp.getWriter()).thenReturn(responseWriter); servlet.doGet(req, resp); verify(resp).setStatus(eq(500)); verify(resp).getWriter(); verify(responseWriter).write(eq("{\"error\":\"error message\"}")); }