AccountService { public Account openAccount(AccountInfo accountInfo) { Account account = new Account(accountInfo); accountRepository.save(account); publishAccountOpenedEvent(account); return account; } AccountService(AccountRepository accountRepository, DomainEventPublisher domainEventPublisher); Account openAccount(AccountInfo accountInfo); Optional<Account> findAccount(long id); void noteCustomerValidated(long accountId); void noteCustomerValidationFailed(long accountId); void debit(long accountId, Money amount); void credit(long accountId, Money amount);  }
@Test public void shouldOpenAccount() { when(accountRepository.save(any(Account.class))).then(invocation -> { ((Account) invocation.getArguments()[0]).setId(accountId); return null; }); AccountInfo accountInfo = new AccountInfo(customerId, "Checking", initialBalance); Account account = accountService.openAccount(accountInfo); verify(accountRepository).save(account); verify(domainEventPublisher).publish(Account.class, accountId, Collections.singletonList(new AccountOpenedEvent(accountInfo))); }