Java Reference
In-Depth Information
One of the most important productivity improvements of the past decade or so has been the
rise of automated testing tools. Java has tools like JUnit and its descendants, which make
both writing and running tests automated and easy.
Testing is another area where Groovy shines. First, the base Groovy libraries include
GroovyTestCase , which extends JUnit's TestCase class and adds a range of helpful
methods, such as testArrayEquals , testToString , and even shouldFail .
Next, Groovy's metaprogramming capabilities have given rise to simple DSLs for testing.
One particularly nice example is the Spock framework, which I'll discuss in chapter 6 on
testing. Spock is lean and expressive, with blocks like given , expect , and when / then .
As an example, consider sorting strings, as implemented in Java and discussed earlier. In
listing 1.3 Ipresented aJavaclass thatsortedstringsbothlexicographically andbydecreas-
ing length. Now I'd like to test that, and to do so I'm going to use the Spock testing frame-
work from Groovy.
A Spock test that checks both sorting methods is shown in the following listing.
Listing 1.4. A Spock test that checks each Java sorting method
Search WWH ::




Custom Search