O'Reilly logo

Programming Groovy 2 by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 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 vs. Mocks, we discussed the difference ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required