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 O’Reilly online learning.

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