Andre's Blog

20160925 Spock Unit Tests For CISystem


The sourcecode of CISystem is growing and I need to start writing unit tests.

Why unit tests? My domain is called "" and I really believe you need unit tests to

  1. test your software
  2. specify your software
  3. document your software

and it reduces the need for documentation, not completely but it helps.

When I have a refactoring project, I first write the unit tests and then start with the refactoring. Doing it this way I know when my refactoring breaks the code.

So, yes, I could have started with the tests before I start with the implementation. Test-Driven-Development really saves time when what you want to do.

For CISystem I first wanted to find out how to do it before I start with the tests. I wanted to find out which classes I need, threading and sockets are new concepts for me.

Before this project I was coding mainly in C++ and I had my own idea of writing unit tests.

Now coding in Java and Groovy I want to use something new, something what is really state of the art.

I have decided to write my unit tests with Spock using gradle to run them.

You run the tests with

gradle test

These are my first two tests, they are not much more than a "Hello, world!", but I am already fascinated how easy it is writing such tests :

import spock.lang.*

class NetworkInitializationTest extends spock.lang.Specification{
    def "setup node sets label 1"() {
        given: " a network object with IP address and port set"

        CINetwork network = new CINetwork();

        and: "setupNode()"


        expect: "label is IPAddress:port"

        network.getLabel() == "";

    def "setup node sets label 2"() {
        given: " a network object"

        CINetwork network = new CINetwork();

        expect: "label is IPAddress:port when setting IP address and port and then calling setupNode()"

        network.getLabel() == A + ":" + B;

        where: "where variations of IP address and port are used"

        A << ["","",""]
        B << ["200","300","1000"]


and the report result is this :


and this is my gradle build script :

apply plugin: 'groovy'
apply plugin: 'java'

repositories {

task fatJar(type: Jar) {

    manifest {
        attributes 'Main-Class': 'com.wartbar.networkinitialization.NetworkInitialization'
    baseName = + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar

dependencies {
    compile ('org.codehaus.groovy:groovy-all:2.4.6',)




  1. I have set up Spock this week for the first time for a project, I had no experience with it before.
  2. From setup to first running test it took about 2 hours.
  3. For CISystem, the second project this week, it took only some minutes.

It is fun using Spock!

These are the sources which have helped me this week :

Spock Primer - Its goals are to teach you enough Spock to write real-world Spock specifications, and to whet your appetite for more.
Petri Kainulainen - Writing Unit Tests With Spock Framework: Creating a Gradle Project

Select where to go ...

The Blog
My Technical Blogs
Blogs Of Friends