Java Reference
In-Depth Information
Figure 6.5. Results of the Spock data-driven tests. The test with the @Unroll annotation is shown in the Eclipse
output as “unrooted,” showing different output messages for each set of data.
Whatiftheclassyouplantotesthasdependencies?Thosedependenciesneedtobestubbed
or mocked, as discussed earlier. Fortunately, Spock has its own mocking capabilities built
in.
6.4.4. The trouble with tribbles
The Specification class from Spock contains a method called Mock that is used to
create mock objects. If your dependency is based on an interface, the Mock method can
generate a mock object directly, using Java's dynamic proxy mechanism. If it's a class,
Mock will extend the class using the CGLIB library.
It's time for a relatively simple (and relatively silly) example. A tribble [ 22 ] is a small, furry
animalthatbreedsprolifically,likesVulcans,andhatesKlingons.Here'sa Tribble class,
written in Groovy.
22 See http://en.wikipedia.org/wiki/The_Trouble_With_Tribbles for details, in the unlikely event you haven't seen
that particular Star Trek (original series) episode. It holds up remarkably well after 35 (!) years.
 
 
Search WWH ::




Custom Search