Java Reference
In-Depth Information
the verify method on StubFor ( MockFor does that automatically). See the API for
details.
The only real limitation on the StubFor and MockFor classes is that they can only be
used to replace Groovy implementations. You can't supply a Java class and have it work.
Still, if your service is implemented in Groovy, they are an invaluable addition to your test-
ing arsenal. [ 13 ]
13 Candor compels me to admit that I worked out how to use StubFor and MockFor over a few days, and then
did what I should have done originally: looked them up in Groovy in Action . GinA (as it was then known; the second
edition is ReGinA) had it all laid out over a few pages, nice and neat. There's a reason that Groovy in Action is still
my all-time favorite technical book.
Lessons learned (mocking dependencies)
1 . To easily create a stub of an interface, use closures to implement the methods. This
is known as closure coercion .
2 . The Expando class has no properties or methods, but both can be added at
runtime to configure an object to do what you want.
3 . The StubFor and MockFor classes in the standard library can be used to create
mock objects even when they're replacing local variables in the test fixture. [ 14 ]
14 If you read nothing else in this chapter, take a look at that.
So far every technique in this chapter has been based on existing classes in the Groovy
standard library. One new testing library, however, has been gaining momentum in the
Groovy community, and not just because it has a clever name. The Spock framework is
simple to learn, easy to use, and the subject of the next section.
6.4. The future of testing: Spock
The Spock framework yields more productivity for less effort than any other framework
I've encountered. Spend a small amount of time with Spock (for example, through the dis-
 
 
Search WWH ::




Custom Search