I have started using the Groovy testing framework called Spock.
It looks like a nice testing framework, but it also seems a bit disorganized. The code is on github, but the main site seems to be on Google Code. They are working on some new documentation, which is incomplete, but has a link to some old documentation. On the old Google Code site, Spock Basics and Getting Started are good places to get started. Someone on the mailing list referred me to some javadocs, some groovydocs, and another set of javadocs. I am not too clear why there are two sets of javadocs. Someday I will get the code from github, and see what I can build myself.
First off, I am not much of a Spock expert. I know I am not using all of the things Spock has to offer. I started porting a DNS server from Java to Groovy, and I decided to start by porting the tests from Java to Groovy. (At this point, very little of the actual server code has been ported. After seeing a lot of byte arrays in the tests, I wonder if writing a DNS server in Groovy is a good idea.) The tests were in JUnit, and after porting them to Groovy, I decided to port them to Spock.
You can find my Groovy Spock tests at the DNS Groovy project here. I have both Spock and JUnit tests in that directory.
I ported the JUnit tests to Spock tests. I extended a couple of classes. I extended org.spockframework.util.ObjectUtil as MyGroovyUtil, and org.spockframework.util.Assert as MyGroovyAssert. I add them to the tests like this:
def mgu = new MyGroovyUtil()
def mga = new MyGroovyAssert()
- assertEquals becomes mgu.equals
- assertNull(XXX) becomes mgu.equals(null, XXX)
- assertTrue becomes mga.assertTrue
- assertFalse(x == y) becomes mga.that( x != y)
- assertFalse(XX.YY) becomes mga.that(!XX.YY)
- assertNotSame(m_h, h2) becomes !mgu.equals(m_h, h2)
- assertSame(x, y) becomes mgu.equals(x, y)
I am not an expert with Spock. I took some code by Venkat S to test the java.util.concurrent.locks interface. I tried porting them from JUnit to Spock, but I was not able to get some of them to work with Spock. Perhaps I will try again in the future. You can find the original JUnit tests in this directory, and my attempts at porting them to Spock are in this directory. Venkat’s code is in this directory. He has a method that adds some values to a map, and he refactors it so that the actual update is in a method called by the put method between the locking and unlocking.