### Question:
UsersServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) { String deletingUserId = req.getParameter("id"); resp.setContentType("text/html"); String message = ""; String error = ""; if (deletingUserId != null) { if (this.validator.delete(Integer.parseInt(deletingUserId))) { message = "User was deleted"; } else { error = "User not found"; } } req.setAttribute("message", message); req.setAttribute("error", error); req.setAttribute("users", this.validator.findAll()); try { req.getRequestDispatcher("/WEB-INF/views/users.jsp").forward(req, resp); } catch (ServletException | IOException e) { LOG.error(e.getMessage(), e); } }    }### Answer:
@Test public void ifDeleteUserThenServletForwardAndSendMessage() throws ServletException, IOException { this.validator.add(new User("test", "test", "test", "user", "pass")); int userId = 0; for (User usr : this.validator.findAll()) { if (usr.getName().equals("test")) { userId = usr.getId(); } } when(this.request.getParameter("id")).thenReturn(String.valueOf(userId)); when(this.request.getRequestDispatcher("/WEB-INF/views/users.jsp")).thenReturn(this.dispatcher); this.testServlet.doPost(this.request, this.response); boolean result = true; for (User usr : this.validator.findAll()) { if (usr.getId() == userId) { result = false; } } assertTrue(result); verify(this.dispatcher).forward(this.request, this.response); verify(this.request).setAttribute("message", "User was deleted"); }
@Test public void ifDeleteNotUserThenServletForwardAndSendError() throws ServletException, IOException { when(this.request.getParameter("id")).thenReturn("-1"); when(this.request.getRequestDispatcher("/WEB-INF/views/users.jsp")).thenReturn(this.dispatcher); this.testServlet.doPost(this.request, this.response); verify(this.dispatcher).forward(this.request, this.response); verify(this.request).setAttribute("error", "User not found"); }