6.3. Mocks: verifying values returned from the class under test

Stubs are great when your class under test already has methods that allow you to understand whether everything works as expected (such as the canShipCompletely() method of the basket class). But most of the time, the only way to understand what happened during the unit test is to have a “log” of what methods were called along with their arguments and their responses.

Mocks are the answer to this need. By mocking a collaborator of the class under test, you not only can preprogram it with canned responses, but also can query it (after the unit test has finished) about all its interactions.

Spock has a huge range of options when it comes to mocks

Spock supports many features ...

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.