@Test public void testGetWithBadId() throws ServletException, IOException { MovieServlet movieServlet = new MovieServlet(); movieServlet.init(); HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); when(request.getPathInfo()).thenReturn("/some_string"); movieServlet.doGet(request, response); verify(response, atLeastOnce()).setStatus(400); // bad request }
@Test public void testGetWithNotExistingId() throws ServletException, IOException { MovieServlet movieServlet = new MovieServlet(); movieServlet.init(); HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); when(request.getPathInfo()).thenReturn("/333"); movieServlet.doGet(request, response); verify(response, atLeastOnce()).setStatus(404); // not found }
private void testGet(MovieServlet movieServlet, long movieId) throws IOException, ServletException { HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); when(request.getPathInfo()).thenReturn("/" + movieId); StringWriter stringWriter = new StringWriter(); PrintWriter writer = new PrintWriter(stringWriter); when(response.getWriter()).thenReturn(writer); movieServlet.doGet(request, response); String responseString = stringWriter.toString(); assertTrue(responseString.contains("\"id\":" + movieId + "," + TEST_MOVIE_JSON)); }