### Question:
SignupController { @RequestMapping(value = CONTROLLER_URL, method = RequestMethod.POST) public String signup(@Valid @ModelAttribute SignupForm signupForm, Errors errors) { if (errors.hasErrors()) { log.warn("Oh no! Signup failed as there are validation errors."); return null; } String hashedPassword = passwordEncoder.encode(signupForm.getPassword()); UserDetails userDetails = new User(signupForm.getName(), hashedPassword, ENABLED); userDetailsManager.createUser(userDetails); log.info("Success! Created new user " + userDetails.getUsername()); return VIEW_SUCCESS; }  @RequestMapping(value = CONTROLLER_URL) SignupForm displayPage(); @RequestMapping(value = CONTROLLER_URL, method = RequestMethod.POST) String signup(@Valid @ModelAttribute SignupForm signupForm, Errors errors); static final String CONTROLLER_URL; static final String VIEW_SUCCESS; }### Answer:
@Test public void testSuccessForward() { SignupForm form = populateForm(NAME, PASSWORD); Errors noErrors = new DirectFieldBindingResult(form, "form"); String response = controller.signup(form, noErrors); assertEquals(SignupController.VIEW_SUCCESS, response); }
@Test public void testAccountCreation() { SignupForm form = populateForm(NAME, PASSWORD); Errors noErrors = new DirectFieldBindingResult(form, "form"); controller.signup(form, noErrors); verify(userDetailsManager).createUser(argThat(hasProperty("username", equalTo(NAME)))); }
@Test public void testPasswordHash() { given(passwordEncoder.encode(PASSWORD)).willReturn(HASHED_PASSWORD); SignupForm form = populateForm(NAME, PASSWORD); Errors noErrors = new DirectFieldBindingResult(form, "form"); controller.signup(form, noErrors); verify(userDetailsManager).createUser(argThat(hasProperty("password", equalTo(HASHED_PASSWORD)))); }
@Test public void testValidationFailIsLogged() { SignupForm invalidForm = populateForm(null, null); Errors errors = new DirectFieldBindingResult(invalidForm, "form"); errors.rejectValue("name", "Invalid name"); errors.rejectValue("password", "Invalid password"); controller.signup(invalidForm, errors); assertThat(sysOut.asString(), containsString("Oh no!")); }
@Test public void testSuccessIsLogged() { SignupForm form = populateForm(NAME, PASSWORD); Errors noErrors = new DirectFieldBindingResult(form, "form"); controller.signup(form, noErrors); assertThat(sysOut.asString(), containsString("Success!")); }