PolicyManagerServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) { LOGGER.debug("doPost"); try { if (isMultipartContent(request)) { uploadFile(request, response); } else { fileOperation(request, response); } } catch (Exception e) { try { setError(e, response); } catch (Exception e1) { LOGGER.error(EXCEPTION_OCCURED + e1); } } }  static synchronized void setPolicyController(PolicyController policyController); static JsonArray getPolicyNames(); static void setPolicyNames(JsonArray policyNames); static List<String> getServiceTypeNamesList(); @Override void init(ServletConfig servletConfig); static void setTestUserId(String testUserId);  }
@Test public void test20DoPostSetErrorException() throws IOException { PolicyManagerServlet servlet = new PolicyManagerServlet(); HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class); HttpServletResponse mockResponse = Mockito.mock(HttpServletResponse.class); doThrow(IOException.class).when(mockRequest).getReader(); doThrow(IOException.class).when(mockResponse).sendError(any(Integer.class), any(String.class)); servlet.doPost(mockRequest, mockResponse); verify(mockRequest).getReader(); }
@Test public void test21DoPostException() throws IOException { PolicyManagerServlet servlet = new PolicyManagerServlet(); HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class); HttpServletResponse mockResponse = Mockito.mock(HttpServletResponse.class); doThrow(IOException.class).when(mockRequest).getReader(); doThrow(IOException.class).when(mockResponse).sendError(any(Integer.class), any(String.class)); doThrow(IOException.class).when(mockResponse).getWriter(); servlet.doPost(mockRequest, mockResponse); verify(mockRequest).getReader(); verify(mockResponse).getWriter(); }
@Test public void test22DoPostSuccess() throws IOException { PolicyManagerServlet servlet = new PolicyManagerServlet(); HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class); HttpServletResponse mockResponse = Mockito.mock(HttpServletResponse.class); PrintWriter mockPrintWriter = Mockito.mock(PrintWriter.class); doThrow(IOException.class).when(mockRequest).getReader(); when(mockResponse.getWriter()).thenReturn(mockPrintWriter); servlet.doPost(null, mockResponse); verify(mockResponse).getWriter(); }
@Test public void test23DoPostUploadFileException() { PolicyManagerServlet servlet = new PolicyManagerServlet() { private static final long serialVersionUID = 1L; @Override protected boolean isMultipartContent(HttpServletRequest request) { return true; } }; HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class); HttpServletResponse mockResponse = Mockito.mock(HttpServletResponse.class); servlet.doPost(mockRequest, mockResponse); }