Spock Notes

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()
The classes that I am extending are abstract. But after extending, I was able to use their methods. Here are a few examples of the methods I used and how I used them to port the test from JUnit:
  • 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 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.


Image from World Digital Library, assumed allowed under Fair Use. Image from the Ashburnham Pentateuch, or Tours Pentateuch, a Latin manuscript of the first five books of the Old Testament from the 6th century or 7th century. Its place of origin is unknown.


2 thoughts on “Spock Notes”

  1. You’re right that the state of the documentation is probably the main thing holding Spock back from wider adoption. I’ve had some success evangelizing its use to non-Groovy folks.

    I’m not sure why you’re using custom assertion methods. Any statement in a `then:` or `expect:` block is automatically an assertion, e.g.

    a == 1

    is effectively the same as JUnit’s

    assertThat(a, equalTo(1))


    assertEquals(1, a)

    in JUnit < 4

    Also you get fantastic diagnostics from allowing Spock's built in asserts which you are likely throwing away by having your own custom assertion methods.

Comments are closed.