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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access