6.4. Putting it all together: credit card charging in two steps

All the examples shown so far illustrate various features of mocks and stubs. I’ll close this chapter with a bigger example that combines most of the techniques shown so far and is closer to what you’d write in a production application.

The fullCheckout()method does the following:

  1. Checks the credit card of the customer. If the card is invalid or doesn’t have enough funds, the method stops there.
  2. If the credit card is OK, the price for the products is reserved from the credit card. (This is called an authorization event in credit card terminology.)
  3. The inventory is checked. If the products are in stock and can be shipped, the amount from the card that was previously reserved is ...

Get Java Testing with Spock now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.