18.10 Mocking Using the Groovy Mock Library

Groovy’s mock library implemented in the ​groovy.mock.interceptor​ package is useful for mocking deeper dependencies—that is, instances of collaborators/dependent objects created within the methods we’re testing. ​StubFor​ and ​MockFor​ are the two classes that take care of this. Let’s look at them one at a time.

StubFor​ and ​MockFor​ are intended to intercept calls to methods like categories do (see Section 18.6, Mocking Using Categories). However, unlike with categories, we don’t have to create separate classes for mocking. We introduce the mock methods on instances of ​StubFor​ or ​MockFor​, and these classes take care of replacing the ​MetaClass​ for the object we’re mocking.

In Stubs ...

Get Programming Groovy 2 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.