Testing a use case with unit tests
Going a layer outward, the next architecture element to test is the use cases implemented as domain services. Let’s look at a test for SendMoneyService, discussed in Chapter 5, Implementing a Use Case. The Send money use case withdraws money from the source account and deposits it into the target account. We want to verify that everything works as expected when the transaction succeeds:
To make the test a little more readable, it’s structured into given/when/then sections, which are commonly used in Behavior-Driven Development.
In the given section, we create the source and target Account objects and put them into the correct state with some methods whose names start with given...(). We also create a SendMoneyCommand object to act as input to the use case. In the when section, we simply call the sendMoney() method to invoke the use case. The then section asserts that the transaction was successful and verifies...