@Test
public void create_UsernameAlreadyExists() {
final Model model = createNiceMock(Model.class);
final UserForm User = new UserForm();
final BindingResult errors = createNiceMock(BindingResult.class);
final String username = "canonical"; //specified username already exists in database
final String password = "password";
final String confirmPassword = password;
final User existingUser = new UserImpl();
List<ObjectError> errorList = new ArrayList<ObjectError>();
final ObjectError error = new ObjectError("username.exists", "Username already exists");
User.setUsername(username);
User.setPassword(password);
User.setConfirmPassword(confirmPassword);
existingUser.setUsername(username);
existingUser.setPassword(password);
errorList.add(error);
expect(errors.hasErrors()).andReturn(true).anyTimes();
expect(errors.getAllErrors()).andReturn(errorList).anyTimes();
replay(errors);
expect(userService.getUserByUsername(username)).andReturn(existingUser).anyTimes();
replay(userService);
replay(model);
String result = new String(newAccountController.create(User, errors, model, request, redirectAttributes));
errorList = errors.getAllErrors();
assertThat(errorList.size(), CoreMatchers.equalTo(1));
assertThat(errorList.get(0).getDefaultMessage(), CoreMatchers.equalTo("Username already exists"));
assertThat(result, CoreMatchers.equalTo(ViewNames.NEW_ACCOUNT));
}