Testing a Use Case with Unit Tests
Going a layer outward, the next architecture element to test is the use cases. Let's look at a test of SendMoneyService, discussed in Chapter 4, Implementing a Use Case. The SendMoney use case locks the source Account so no other transactions can change its balance in the meantime. If we can successfully withdraw money from the source account, we lock the target account as well and deposit the money there. Finally, we unlock both accounts again.
We want to verify that everything works as expected when the transaction succeeds:
class SendMoneyServiceTest {
// declaration of fields omitted
@Test
void transactionSucceeds() {
Account sourceAccount = givenSourceAccount();
Account targetAccount = givenTargetAccount();
givenWithdrawalWillSucceed(sourceAccount);
givenDepositWillSucceed(targetAccount);
...