### Question:
AccountServiceImpl implements AccountService { @Override public UserDetails authenticate(final String username, final String password) throws AccountAuthenticationException, AccountNotValidatedException { final String url = String.format(AUTHENTICATE_URL, serviceProperties.getAccountServiceUrl(), LocaleContextHolder.getLocale().getLanguage()); final AuthDto authDto = new AuthDto(); authDto.setUsername(username); authDto.setPassword(password); final ResponseEntity<AccountResultModel> response; try { response = restTemplate.postForEntity(url, authDto, AccountResultModel.class); } catch (final Exception ex) { final String errorMessage = String.format("An error occured while authenticating account %s: %s", username, ex.getMessage()); log.error(errorMessage, ex); throw new AccountAuthenticationException(errorMessage, ex); } if (response.getStatusCode().equals(HttpStatus.BAD_REQUEST)) { throw new AccountNotValidatedException(); } final AccountDto accountDto = response.getBody().getResult(); return new AuthenticatedUser( accountDto.getUsername(), password, accountDto.getEmailAddress(), new ArrayList<>()); }  @Override UserDetails authenticate(final String username, final String password); @Override UserDetails loadUserByUsername(final String username); @Override AccountResultModel createAccount(final AccountDto accountDto); @Override AccountResultModel changePassword(final String username, final ChangePasswordModel changePasswordModel);  }### Answer:
@Test public void test_authenticate_success() throws Exception { final AuthDto authDto = new AuthDto(); authDto.setUsername(USERNAME); authDto.setPassword(PASSWORD); final AccountResultModel expectedResult = new AccountResultModel(); expectedResult.setResult(new AccountDto()); expectedResult.getResult().setUsername(USERNAME); expectedResult.getResult().setEmailAddress(EMAIL_ADDRESS); when(response.getBody()).thenReturn(expectedResult); when(response.getStatusCode()).thenReturn(HttpStatus.OK); when(restTemplate.postForEntity(ACCOUNT_SERVICE_URL + ACCOUNT_AUTH_PATH, authDto, AccountResultModel.class)) .thenReturn(response); final AuthenticatedUser result = (AuthenticatedUser) accountService.authenticate(USERNAME, PASSWORD); assertEquals(USERNAME, result.getUsername()); assertEquals(PASSWORD, result.getPassword()); assertEquals(EMAIL_ADDRESS, result.getEmailAddress()); }
@Test(expected = AccountNotValidatedException.class) public void test_authenticate_fail() throws Exception { final AuthDto authDto = new AuthDto(); authDto.setUsername(USERNAME); authDto.setPassword(PASSWORD); when(response.getStatusCode()).thenReturn(HttpStatus.BAD_REQUEST); when(restTemplate.postForEntity(ACCOUNT_SERVICE_URL + ACCOUNT_AUTH_PATH, authDto, AccountResultModel.class)) .thenReturn(response); accountService.authenticate(USERNAME, PASSWORD); }
@Test(expected = AccountAuthenticationException.class) public void test_authenticate_exception() throws Exception { final AuthDto authDto = new AuthDto(); authDto.setUsername(USERNAME); authDto.setPassword(PASSWORD); when(restTemplate.postForEntity(ACCOUNT_SERVICE_URL + ACCOUNT_AUTH_PATH, authDto, AccountResultModel.class)) .thenThrow(new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR)); accountService.authenticate(USERNAME, PASSWORD); }