### Question:
AccountServiceImpl implements AccountService { @Override public AccountDto updateAccount(final AccountDto accountDto) throws AccountNotFoundException { final Account account = accountRepository.findById(accountDto.getUsername()).orElse(null); if (account == null) { throw new AccountNotFoundException(); } account.setPasswordSalt(BCrypt.gensalt()); account.setPassword(BCrypt.hashpw(accountDto.getPassword(), account.getPasswordSalt())); account.setUpdateTimestamp(Instant.now().toEpochMilli()); try { accountRepository.save(account); } catch (final Exception ex) { final String errorMessage = String.format("An error occured while updating account %s: %s", account.getUsername(), ex.getMessage()); log.error(errorMessage, ex); throw new AccountPersistenceException(errorMessage, ex); } return accountMapper.accountToAccountDto(account); }  @Override Optional<AccountDto> getAccount(final String username); @Override Optional<AccountDto> getAccount(final String username, final String password); @Override AccountDto createAccount(final AccountDto accountDto); @Override AccountDto updateAccount(final AccountDto accountDto); @Override void deleteAccount(final String username);  }### Answer:
@Test(expected = AccountNotFoundException.class) public void test_updateAccount_accountNotFound() throws Exception { final AccountDto accountDto = new AccountDto(); when(accountRepository.findById(accountDto.getUsername())).thenReturn(Optional.empty()); accountService.updateAccount(accountDto); }
@Test public void test_updateAccount_successfulSave() throws Exception { final AccountDto accountDto = new AccountDto(); accountDto.setPassword(TEST_PASSWORD); final Account account = mock(Account.class); when(accountRepository.findById(accountDto.getUsername())).thenReturn(Optional.of(account)); when(account.getPasswordSalt()).thenReturn(BCrypt.gensalt()); accountService.updateAccount(accountDto); verify(accountRepository).save(account); verify(account, never()).setUsername(anyString()); verify(account, never()).setEmailAddress(anyString()); verify(account).setPassword(anyString()); verify(account).setPasswordSalt(anyString()); verify(account, never()).setCreationTimestamp(anyLong()); verify(account).setUpdateTimestamp(anyLong()); }
@Test(expected = AccountPersistenceException.class) public void test_updateAccount_runtimeException() throws Exception { final AccountDto accountDto = new AccountDto(); accountDto.setPassword(TEST_PASSWORD); final Account account = mock(Account.class); when(accountRepository.findById(accountDto.getUsername())).thenReturn(Optional.of(account)); when(account.getPasswordSalt()).thenReturn(BCrypt.gensalt()); when(accountRepository.save(account)).thenThrow(new RuntimeException()); accountService.updateAccount(accountDto); }