8.3. Creating partial mocks with spies

In this section you’ll see how to create partial mocks.[8] Chapter 6 explained how Spock can create fake objects that are useful for testing and showed you mocks and stubs. Spock supports a third type of “fake” object: spies.

8

And why you shouldn’t use them!

Spies, shown in figure 8.5, work as partial mocks. They take over a Java object and mock only some of its methods. Method calls can either by stubbed (like mocks) or can pass through to the real object.

Figure 8.5. A spy is a real class in which only a subset of methods are fake. The rest are the real methods.

I purposely didn’t show you spies in ...

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.