Chapter 6. Mocking and stubbing

This chapter covers

  • A quick introduction to fake collaborators
  • Instructing Spock stubs with canned responses
  • Verifying interactions with Spock mocks
  • Verifying arguments or return values of the class under test

This chapter starts with a quick reminder of the theory behind mocks and stubs (the terminology used by Spock). If you’ve never used them before in your unit tests, feel free to consult other external sources to complete your knowledge.[1] You may have already seen them as test doubles or fake collaborators, so if you know the theory, you can skip ahead to section 6.2 in order to see the implementation of Spock for fake objects.


A good start is the book Effective Unit Testing by Lasse Koskela (Manning, ...

Get Java Testing with Spock now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.