@Test
public void testCreate() throws Exception {
Model model = createNiceMock(Model.class);
NewUser newUser = new NewUser();
BindingResult results = new DirectFieldBindingResult(newUser, ModelKeys.NEW_USER);
RedirectAttributes redirectAttributes = createNiceMock(RedirectAttributes.class);
replay(redirectAttributes);
replay(model);
// user part
// required email
controller.requestUsername(newUser, results, model, request, redirectAttributes);
assertThat(captchaService.isValid(request), CoreMatchers.equalTo(true));
assertEquals("Expected email errors", 1, results.getErrorCount());
assertEquals("Expected email errors", "email.required", results.getFieldError().getCode());
// invalid email
results = new DirectFieldBindingResult(newUser, ModelKeys.NEW_USER);
newUser.setEmail("test_email");
controller.requestUsername(newUser, results, model, request, redirectAttributes);
assertEquals("Expected email errors", "email.invalid", results.getFieldError().getCode());
// does not exists
results = new DirectFieldBindingResult(newUser, ModelKeys.NEW_USER);
newUser.setEmail("test@mail.com");
String viewResult = controller.requestUsername(newUser, results, model, request, redirectAttributes);
assertEquals("Expected email errors", 1, results.getErrorCount());
assertEquals("Expected email errors", "email.doesnot.exist", results.getFieldError().getCode());
assertThat(viewResult, CoreMatchers.equalTo(ViewNames.USERNAME_REQUEST));
// password part:
model = createNiceMock(Model.class);
newUser = new NewUser();
results = new DirectFieldBindingResult(newUser, ModelKeys.NEW_USER);
redirectAttributes = createNiceMock(RedirectAttributes.class);
replay(redirectAttributes);
replay(model);
// required email
controller.requestPassword(newUser, results, model, request, redirectAttributes);
assertThat(captchaService.isValid(request), CoreMatchers.equalTo(true));
assertEquals("Expected email errors", 1, results.getErrorCount());
assertEquals("Expected email errors", "email.required", results.getFieldError().getCode());
// invalid email
results = new DirectFieldBindingResult(newUser, ModelKeys.NEW_USER);
newUser.setEmail("test_email");
controller.requestPassword(newUser, results, model, request, redirectAttributes);
assertEquals("Expected email errors", "email.invalid", results.getFieldError().getCode());
// does not exists
results = new DirectFieldBindingResult(newUser, ModelKeys.NEW_USER);
newUser.setEmail("test@mail.com");
viewResult = controller.requestPassword(newUser, results, model, request, redirectAttributes);
assertEquals("Expected email errors", 1, results.getErrorCount());
assertEquals("Expected email errors", "email.doesnot.exist", results.getFieldError().getCode());
assertThat(viewResult, CoreMatchers.equalTo(ViewNames.NEW_PASSWORD_REQUEST));
}