PasswordChangeController { @RequestMapping(method = RequestMethod.POST) public ResponseEntity<RestResponse> saveOrUpdate( @RequestBody @Valid PasswordChangeDTO passwordChangeDTO, BindingResult bindingResult, @CurrentUser UserDetailsAdapter userDetailsAdapter) { if (!bindingResult.hasErrors()) { try { passwordValidator.validate(passwordChangeDTO); String newPassword = bCryptPasswordEncoder.encode(passwordChangeDTO.getNewPassword()); PasswordHistoryDTO passwordHistoryDTO = PasswordHistoryDTO.builder() .userId(userDetailsAdapter.getUserId()) .password(newPassword) .changeDate(new Date()) .build(); UserDTO user = userService.findUserByName(userDetailsAdapter.getUsername()); user.setPassword(newPassword); userService.saveOrUpdate(user); userDetailsAdapter.setPassword(newPassword); passwordChangeService.saveOrUpdate(passwordHistoryDTO); } catch (PasswordValidationException e) { logger.error(e.getMessage(), e); return new ResponseEntity<>( new RestResponse(messageKeyResolver .resolveMessageOrDefault( ruleValidationExceptionMapper.getMessageKeyByException(e))), HttpStatus.BAD_REQUEST); } return new ResponseEntity<>(RestResponse.NO_ERROR, HttpStatus.OK); } else { String errorMessage = messageKeyResolver.resolveMessageOrDefault(LOGIN_BACKEND_VALIDATION); RestResponse restResponse = new RestResponse(errorMessage); return new ResponseEntity<>(restResponse, HttpStatus.BAD_REQUEST); } }  @RequestMapping(method = RequestMethod.POST) ResponseEntity<RestResponse> saveOrUpdate(
      @RequestBody @Valid PasswordChangeDTO passwordChangeDTO,
      BindingResult bindingResult,
      @CurrentUser UserDetailsAdapter userDetailsAdapter); static final String LOGIN_BACKEND_VALIDATION; }
@Test public void saveOrUpdateShouldRespondWithNoErrorResponseWhenThePostedJsonIsCorrect() throws Exception { given(userService.findUserByName(anyString())).willReturn(DUMMY_USER_DTO); mockMvc.perform(buildJsonPostRequest(REQUEST_URL, CORRECT_PASSWORD_CHANGE_DTO)) .andExpect(status().isOk()) .andExpect(jsonPath("$.errorMessage").isEmpty()) .andExpect(jsonPath("$.fields").isEmpty()); then(passwordValidator).should().validate(CORRECT_PASSWORD_CHANGE_DTO); then(userService).should().findUserByName(USER_NAME); then(userService).should().saveOrUpdate(DUMMY_USER_DTO); ArgumentCaptor<PasswordHistoryDTO> historyCaptor = ArgumentCaptor.forClass(PasswordHistoryDTO.class); verify(passwordChangeService).saveOrUpdate(historyCaptor.capture()); verifyZeroInteractions(messageKeyResolver); }