@Test
public void testUpdate() throws Exception {
final Model model = createNiceMock(Model.class);
RedirectAttributes redirectAttributes = createNiceMock(RedirectAttributes.class);
NewUser newUser = new NewUser();
replay(redirectAttributes);
replay(model);
BindingResult results = new DirectFieldBindingResult(newUser, ModelKeys.NEW_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));
// invalid password, to short:
newUser.setPassword("123");
results = new DirectFieldBindingResult(newUser, ModelKeys.NEW_USER);
controller.update(newUser, results, model, redirectAttributes);
assertEquals("Expected password errors", 2, results.getErrorCount());
assertEquals("Expected password errors", "password.invalid.length", results.getFieldError().getCode());
// missing password confirm:
newUser.setPassword("1234");
results = new DirectFieldBindingResult(newUser, ModelKeys.NEW_USER);
controller.update(newUser, results, model, redirectAttributes);
assertEquals("Expected password errors", 1, results.getErrorCount());
assertEquals("Expected password errors", "confirmPassword.required", results.getFieldError().getCode());
// password confirm not equal:
newUser.setPassword("1234");
newUser.setConfirmPassword("12345");
results = new DirectFieldBindingResult(newUser, ModelKeys.NEW_USER);
controller.update(newUser, results, model, redirectAttributes);
assertEquals("Expected password errors", 1, results.getErrorCount());
assertEquals("Expected password errors", "confirmPassword.mismatch", results.getFieldError().getCode());
// ok request
newUser.setPassword("1234");
newUser.setConfirmPassword("1234");
results = new DirectFieldBindingResult(newUser, ModelKeys.NEW_USER);
controller.update(newUser, results, model, redirectAttributes);
assertEquals("Expected password errors", 0, results.getErrorCount());