6.1. Using fake collaborators
Let’s say you have a Java class in your application that shuts down a nuclear reactor when radiation levels from a sensor pass a critical threshold. You want to write a unit test for that class. It wouldn’t be realistic to bombard the real sensor with radiation just to see your unit test work. It’s also not realistic to shut down the nuclear reactor whenever the unit test runs.
For a more run-of-the-mill example, assume that your Java class sends a reminder email to a customer whenever an invoice isn’t settled on time. Re-creating a delayed payment in the unit test would be difficult, and sending a real email each time the unit test runs would also be unrealistic.
For cases like this, you need to employ fake collaborators ...
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.