### Question:
GeneratePages extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(true); String fileName = null; String path = null; FileWriter file = null; File f; PrintWriter pw = null; InputStream in = null; OutputStream out = null; byte[] buf = new byte[1024]; int len; fileName = request.getParameter("wsId"); fileName = fileName.substring(fileName.lastIndexOf("/")); fileName = fileName.substring(fileName.lastIndexOf("/")+1,fileName.lastIndexOf("?")); fileName = fileName+".jsp"; path = request.getSession().getServletContext().getRealPath("/"); in = new FileInputStream(path + "interface.jsp"); out =new FileOutputStream(path+fileName); System.out.println("This is the input file: " + path + "interface.jsp"); System.out.println("This is the output file: " + path + fileName); while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } String wsdlURL = null; if (request.getParameter("wsId") != null) { wsdlURL = request.getParameter("wsId"); } else { } RequestDispatcher rd = getServletContext().getRequestDispatcher( "/"+fileName); rd.forward(request, response); } GeneratePages(); @Override void init(ServletConfig config); static String configLocation; }### Answer:
@Test public void testDoPost() throws Exception { HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); HttpSession sesion = mock(HttpSession.class); ServletConfig config = mock(ServletConfig.class); ServletContext context = mock(ServletContext.class); RequestDispatcher dispatcher = mock(RequestDispatcher.class); ServletOutputStream stream = mock(ServletOutputStream.class); when(config.getServletContext()).thenReturn(context); when(sesion.getServletContext()).thenReturn(context); when(context.getRequestDispatcher("/prueba.jsp")).thenReturn(dispatcher); URL url = this.getClass().getResource("/prueba.txt"); File testFile = new File(url.getFile()); when(context.getRealPath("/")).thenReturn(testFile.getParent()+"/"); when(request.getParameter("wsId")).thenReturn("/prueba?"); when(request.getSession()).thenReturn(sesion); when(request.getSession(true)).thenReturn(sesion); when(response.getOutputStream()).thenReturn(stream); GeneratePages pages = new GeneratePages(); try { pages.init(config); pages.doPost(request, response); } catch (ServletException ex) { fail("Should not raise exception " + ex.toString()); } catch (IOException ex) { fail("Should not raise exception " + ex.toString()); } }