### Question:
ChangePasswordController { @RequestMapping(value = {"/changepassword", "/changepassword/**"}, method = RequestMethod.POST) public String update(@ModelAttribute UserForm user, BindingResult results, Model model, RedirectAttributes redirectAttributes) { log.debug("updating user password for hash {}", user.getForgotPasswordHash()); model.addAttribute(ModelKeys.USER, user); passwordValidator.validate(user, results); if (results.hasErrors()) { log.info("changepassword, request contains validation errors"); return ViewNames.PASSWORD_CHANGE; } try { log.debug("Submitted passwords were valid"); userService.updatePassword(ModelUtils.convert(user)); redirectAttributes.addFlashAttribute(ModelKeys.REDIRECT_MESSAGE, messagePasswordChanged); return ViewNames.REDIRECT_LOGIN ; } catch (Exception ex) { results.reject("Unable to change password:" + ex.getMessage(), "Unable to change password."); return ViewNames.PASSWORD_CHANGE; } } @Autowired  ChangePasswordController(UserService userService, ChangePasswordValidator passwordValidator); @RequestMapping(value = {"/changepassword/{passwordHash:.*}"}, method = RequestMethod.GET) String initialize(Model model, @PathVariable("passwordHash") String passwordHash); @RequestMapping(value = {"/changepassword", "/changepassword/**"}, method = RequestMethod.POST) String update(@ModelAttribute UserForm user, BindingResult results, Model model, RedirectAttributes redirectAttributes);  }### Answer:
@Test public void testUpdate() throws Exception { final Model model = createNiceMock(Model.class); RedirectAttributes redirectAttributes = createNiceMock(RedirectAttributes.class); UserForm newUser = new UserForm(); replay(redirectAttributes); replay(model); BindingResult results = new DirectFieldBindingResult(newUser, ModelKeys.USER); String viewName = controller.update(newUser, results, model, redirectAttributes); assertThat(viewName, CoreMatchers.equalTo(ViewNames.PASSWORD_CHANGE)); assertThat(results.hasErrors(), CoreMatchers.equalTo(true)); assertThat(results.getErrorCount(), CoreMatchers.equalTo(2)); newUser.setPassword("123"); results = new DirectFieldBindingResult(newUser, ModelKeys.USER); controller.update(newUser, results, model, redirectAttributes); assertEquals("Expected password errors", 2, results.getErrorCount()); assertEquals("Expected password errors", "password.invalid.length", results.getFieldError().getCode()); newUser.setPassword("1234"); results = new DirectFieldBindingResult(newUser, ModelKeys.USER); controller.update(newUser, results, model, redirectAttributes); assertEquals("Expected password errors", 1, results.getErrorCount()); assertEquals("Expected password errors", "confirmPassword.required", results.getFieldError().getCode()); newUser.setPassword("1234"); newUser.setConfirmPassword("12345"); results = new DirectFieldBindingResult(newUser, ModelKeys.USER); controller.update(newUser, results, model, redirectAttributes); assertEquals("Expected password errors", 1, results.getErrorCount()); assertEquals("Expected password errors", "confirmPassword.mismatch", results.getFieldError().getCode()); newUser.setPassword("1234"); newUser.setConfirmPassword("1234"); results = new DirectFieldBindingResult(newUser, ModelKeys.USER); controller.update(newUser, results, model, redirectAttributes); assertEquals("Expected password errors", 0, results.getErrorCount()); }