### Question:
CustomerServiceImpl implements CustomerService { @Override @Transactional(readOnly = true) public Optional<Customer> getCustomerByUsername(String username) { Optional<Account> accountOptional = accountDAO.findByUsername(username); return accountOptional.flatMap(account -> customerDAO.findById(account.getUser().getId())); }  @Override @Transactional(readOnly = true) Optional<Customer> getCustomerByUsername(String username); @Override Customer createNewCustomer(); @Override @Transactional void updateCustomer(Customer customer); CustomerDAO getCustomerDAO(); @Autowired void setCustomerDAO(CustomerDAO customerDAO); AccountDAO getAccountDAO(); @Autowired void setAccountDAO(AccountDAO accountDAO);  }### Answer:
@Test public void shouldGetCustomerByUsername() throws Exception { String username = "john.doe"; Long customerId = 42L; Customer customer = new Customer(); customer.setId(customerId); Account account = new Account(); account.setUser(customer); Mockito.when(accountDAO.findByUsername(username)).thenReturn(Optional.of(account)); Mockito.when(customerDAO.findById(customerId)).thenReturn(Optional.of(customer)); Optional<Customer> result = userService.getCustomerByUsername(username); assertThat(result).contains(customer); }
@Test public void shouldReturnEmptyOptionalWhenAccountIsNotFound() throws Exception { Mockito.when(accountDAO.findByUsername(ArgumentMatchers.anyString())).thenReturn(Optional.empty()); Optional<Customer> result = userService.getCustomerByUsername("john"); assertThat(result).isEmpty(); }