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.

1

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

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.