Java Reference
In-Depth Information
def stub = new StubFor(XmlSlurper)
stub.demand.parse { correctRoot }
The StubFor constructor takes a class reference and builds a stub around it. Then I “de-
mand” that the parse method return the root of the tree calculated in the previous frag-
ment.
To get Groovy to use the new stub, invoke the test inside a closure argument to the use
method:
stub.use {
geocoder.fillInLatLng(stadium)
}
The use closure is the key. Through the magic of Groovy metaprogramming, when the
parse method of XmlSlurper is accessed inside the fillInLatLng method, the de-
manded version is used rather than the actual implementation. The result is that the busi-
ness logic of the fillInLatLng method is tested, without relying on the slurper itself.
The next listing shows the complete test. To make absolutely sure that the online version of
the geocoder is not being used, I created a stadium with the wrong address. The only way
the test passes is if the slurper returns the rigged values.
Search WWH ::




Custom Search