### Question:
AccountController { @GetMapping("/account/changePassword") public String changePassword(final Model model) { return getChangePasswordPage(model, new ChangePasswordModel(), Collections.emptyList()); }  @GetMapping("/account/register") String register(final Model model); @PostMapping("/account/register") String register(final RedirectAttributes redirectAttributes, @ModelAttribute final AccountDto accountDto,
        final Model model); @GetMapping("/account/changePassword") String changePassword(final Model model); @PostMapping("/account/changePassword") String changePassword(
        final RedirectAttributes redirectAttributes,
        final Authentication authentication,
        @ModelAttribute @Valid final ChangePasswordModel changePasswordModel,
        final BindingResult bindingResult,
        final Model model);  }### Answer:
@Test public void test_changePassword_post_serviceErrors() throws Exception { final AccountResultModel result = new AccountResultModel(); result.setErrors(Arrays.asList(VALIDATION_ERROR_MESSAGE)); final ChangePasswordModel changePasswordModel = new ChangePasswordModel(); changePasswordModel.setCurrentPassword(TEST_PASSWORD); changePasswordModel.setNewPassword(TEST_NEW_PASSWORD); when(accountService.changePassword(TEST_AUTHENTICATED_USER, changePasswordModel)) .thenReturn(result); mockMvc.perform(post("/account/changePassword") .with(csrf()) .with(user(TEST_AUTHENTICATED_USER)) .param("currentPassword", TEST_PASSWORD) .param("newPassword", TEST_NEW_PASSWORD)) .andExpect(status().isOk()) .andExpect(model().attributeExists("model")) .andExpect(model().attribute("errors", hasItem(VALIDATION_ERROR_MESSAGE))) .andExpect(view().name("account/changePassword")); }
@Test public void test_changePassword_post_authenticationException() throws Exception { final ChangePasswordModel changePasswordModel = new ChangePasswordModel(); changePasswordModel.setCurrentPassword(TEST_PASSWORD); changePasswordModel.setNewPassword(TEST_NEW_PASSWORD); when(accountService.changePassword(TEST_AUTHENTICATED_USER, changePasswordModel)) .thenThrow(new AccountNotValidatedException()); mockMvc.perform(post("/account/changePassword") .with(csrf()) .with(user(TEST_AUTHENTICATED_USER)) .param("currentPassword", TEST_PASSWORD) .param("newPassword", TEST_NEW_PASSWORD)) .andExpect(status().isOk()) .andExpect(model().attributeExists("model")) .andExpect(model().attribute("errors", hasItem(MESSAGE))) .andExpect(view().name("account/changePassword")); }
@Test public void test_changePassword_post_generalException() throws Exception { final ChangePasswordModel changePasswordModel = new ChangePasswordModel(); changePasswordModel.setCurrentPassword(TEST_PASSWORD); changePasswordModel.setNewPassword(TEST_NEW_PASSWORD); when(accountService.changePassword(TEST_AUTHENTICATED_USER, changePasswordModel)) .thenThrow(new RuntimeException()); mockMvc.perform(post("/account/changePassword") .with(csrf()) .with(user(TEST_AUTHENTICATED_USER)) .param("currentPassword", TEST_PASSWORD) .param("newPassword", TEST_NEW_PASSWORD)) .andExpect(status().isOk()) .andExpect(model().attributeExists("model")) .andExpect(model().attribute("errors", hasItem(MESSAGE))) .andExpect(view().name("account/changePassword")); }
@SuppressWarnings("unchecked") @Test public void test_changePassword_post_success() throws Exception { final AccountResultModel result = new AccountResultModel(); result.setResult(new AccountDto()); result.setErrors(Collections.emptyList()); final ChangePasswordModel changePasswordModel = new ChangePasswordModel(); changePasswordModel.setCurrentPassword(TEST_PASSWORD); changePasswordModel.setNewPassword(TEST_NEW_PASSWORD); when(accountService.changePassword(TEST_AUTHENTICATED_USER, changePasswordModel)) .thenReturn(result); mockMvc.perform(post("/account/changePassword") .with(csrf()) .with(user(TEST_AUTHENTICATED_USER)) .param("currentPassword", TEST_PASSWORD) .param("newPassword", TEST_NEW_PASSWORD)) .andExpect(status().is3xxRedirection()) .andExpect(flash().attribute("notifications", hasItems())) .andExpect(redirectedUrl("/")); }