HelloJettyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletOutputStream out = response.getOutputStream(); out.write("hello".getBytes()); out.flush(); out.close(); }    }
@Test public void testDoGet() throws ServletException, IOException { final HttpServletRequest request = mock(HttpServletRequest.class); final HttpServletResponse response = mock(HttpServletResponse.class); when(response.getStatus()).thenReturn(200); when(response.getOutputStream()).thenReturn(new ServletOutputStream() { @Override public void write(final byte b[]) throws IOException { assertThat(new String(b), is("hello")); } @Override public void write(final int i) throws IOException {} @Override public boolean isReady() { return false; } @Override public void setWriteListener(final WriteListener writeListener) {} }); HelloJettyServlet servlet = new HelloJettyServlet(); servlet.doGet(request, response); assertThat(response.getStatus(), is(200)); }