### Question:
UserUpdateServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) { User updatedUser = new User( req.getParameter("name"), req.getParameter("login"), req.getParameter("email"), req.getParameter("role"), req.getParameter("password") ); updatedUser.setCity(req.getParameter("city")); updatedUser.setCountry(req.getParameter("country")); updatedUser.setId(Integer.parseInt(req.getParameter("id"))); String message = ""; String error = ""; if (this.validator.update(updatedUser)) { message = "User successfully updated"; } else { error = "User not updated"; } req.setAttribute("message", message); req.setAttribute("error", error); try { req.setAttribute("users", this.validator.findAll()); req.getRequestDispatcher("/WEB-INF/views/users.jsp").forward(req, resp); } catch (ServletException | IOException e) { LOG.error(e.getMessage(), e); } }    }### Answer:
@Test public void ifUpUserThenUserShouldBeUpdate() throws ServletException, IOException { this.validator.add(new User("updateTest", "login", "email", "administrator", "test")); int userId = 0; Calendar userDate = null; for (User user : this.validator.findAll()) { if (user.getName().equals("updateTest")) { userId = user.getId(); userDate = user.getCreateDate(); } } User expect = new User("testName", "testLogin", "testMail", "user", "pass"); expect.setId(userId); expect.setCreateDate(userDate); when(this.request.getParameter("id")).thenReturn(String.valueOf(userId)); when(this.request.getParameter("name")).thenReturn(expect.getName()); when(this.request.getParameter("login")).thenReturn(expect.getLogin()); when(this.request.getParameter("email")).thenReturn(expect.getEmail()); when(this.request.getParameter("role")).thenReturn(expect.getRole()); when(this.request.getParameter("password")).thenReturn(expect.getPassword()); when(this.request.getRequestDispatcher("/WEB-INF/views/users.jsp")).thenReturn(dispatcher); this.testServlet.doPost(this.request, this.response); User afterUp = this.validator.findById(userId); verify(this.dispatcher).forward(this.request, this.response); verify(this.request).setAttribute("message", "User successfully updated"); assertThat(afterUp, is(expect)); }
@Test public void ifUpNotExistentUserThenServletShouldBeForwardWithMessage() throws ServletException, IOException { when(this.request.getParameter("id")).thenReturn("0"); 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 updated"); }