### Question:
AccountServiceImpl implements AccountService { @Override public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException { final String url = String.format(GET_URL, serviceProperties.getAccountServiceUrl(), username, LocaleContextHolder.getLocale().getLanguage()); final ResponseEntity<AccountResultModel> response; try { response = restTemplate.getForEntity(url, AccountResultModel.class); } catch (final Exception ex) { final String errorMessage = String.format("An error occured while loading account %s: %s", username, ex.getMessage()); log.error(errorMessage, ex); throw new UsernameNotFoundException(errorMessage, ex); } if (response.getStatusCode().equals(HttpStatus.NOT_FOUND)) { throw new UsernameNotFoundException(String.format("Account %s was not found.", username)); } final AccountDto accountDto = response.getBody().getResult(); return new AuthenticatedUser( accountDto.getUsername(), accountDto.getPassword(), 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(expected = UsernameNotFoundException.class) public void test_loadUserByUsername_exception() throws Exception { final String url = ACCOUNT_SERVICE_URL + String.format(ACCOUNT_GET_PATH, USERNAME); when(restTemplate.getForEntity(url, AccountResultModel.class)) .thenThrow(new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR)); accountService.loadUserByUsername(USERNAME); }
@Test public void test_loadUserByUsername_success() throws Exception { final AccountResultModel expectedResult = new AccountResultModel(); expectedResult.setResult(new AccountDto()); expectedResult.getResult().setUsername(USERNAME); expectedResult.getResult().setEmailAddress(EMAIL_ADDRESS); final String url = ACCOUNT_SERVICE_URL + String.format(ACCOUNT_GET_PATH, USERNAME); when(response.getBody()).thenReturn(expectedResult); when(response.getStatusCode()).thenReturn(HttpStatus.OK); when(restTemplate.getForEntity(url, AccountResultModel.class)) .thenReturn(response); final AuthenticatedUser result = (AuthenticatedUser) accountService.loadUserByUsername(USERNAME); assertEquals(USERNAME, result.getUsername()); assertEquals(EMAIL_ADDRESS, result.getEmailAddress()); }
@Test(expected = UsernameNotFoundException.class) public void test_loadUserByUsername_notFound() throws Exception { final String url = ACCOUNT_SERVICE_URL + String.format(ACCOUNT_GET_PATH, USERNAME); when(response.getStatusCode()).thenReturn(HttpStatus.NOT_FOUND); when(restTemplate.getForEntity(url, AccountResultModel.class)) .thenReturn(response); accountService.loadUserByUsername(USERNAME); }