Building Better Software Tests With Spock

Development Blog

Building Better Software Tests With the Spock Testing Framework

6 Minute Read | October 14th, 2016

Gerd Rohleder

Gerd Rohleder
Senior Java Developer

Development is one of the key activities that build, enable and ultimately power the overall mobile environment.  Today we are sharing some key insight into an important aspect of development – testing framework – from one of our most experienced developers at Smaato. Developers know that choosing the right testing framework for the job can be really complicated. The jUnit Framework is an excellent Java testing tool, but for specific tests that require sample data, better tools – like the Groovy-based Spock  – are available. This post describes how to use the Spock testing framework and how it handles test data. It also explores how Spock tests can be executed with jUnit tests and maven.

How jUnit Runs Parameterized Tests

To have a test that works with a set of test data, you need to define a data method which returns all samples in a Collection. Each sample is used to instantiate a new test class. The constructor needs suitable parameters for the test samples. Then all test methods are executed, as you can see in the code samples below:

package spocky;


public class SayHelloTest {

private final SayHello hello = new SayHello();

private final String toWho;

private final String expectedResult;


@Parameters(name = "Say Hello to {0}")

public static Collection<Object[]> data() {

return Arrays.asList(new Object[][] {{"Dr. Who", "hello Dr. Who"},

   {"world”, "hello world" }});



public SayHello(final String toWho, final String expectedResult){

this.toWho = toWho;

this.expectedResult = expectedResult;



public void sayHello(){

final String result = hello.speak(toWho);

assertThat(result, is(expectedResult));



public void saySomethingElse() throws Exception {





JUnit/Spock Testing Framework
Here we can see some major problems with jUnit parameterized tests:

  • Constructor and attributes are required (boiler code)
  • A separate runner is required
  • A test is executed for every data sample (wasteful for tests without sample data)
  • Only one set of sample data is allowed per test class

Certain test frameworks like JUnitParams offer different approaches that make tests more readable. But I was unable to find a pure Java-based approach that fit the bill, so I looked around for a solution and found the Spock testing framework.

How to Use Spock Testing Framework

Spock is a Groovy-based test framework. It uses the jUnit TestRunner, which means you can code in your favorite IDE and build tools to write and execute Spock specifications.

Project Setup

These maven dependencies are required at setup:












We also need a /src/test/groovy directory in our project, which will contain all Groovy-related files.

“Hello World”

With Spock, you write specifications that describe the features your system has (or should have).

Each specification must extend from the spock.lang.Specification and should be named with “Specification” as a postfix. This detail will be important later in the build process.

import spock.lang.Specification

class HelloWorldSpecification extends Specification {

def "say hello to the world"(){



Feature methods are named with String literals – this way, you can write more readable names. Each feature has the same phases as any unit test:

  1. Setup (optional)
  2. Execution
  3. Verification
  4. Clean up (optional)

Here’s a short “hello world” example of how to implement these phases with Spock:

def "say hello to the world"(){


def helloWorld = new HelloWorld()


def result = helloWorld.speak()


result == "hello world"


In this example, we use three blocks to define our feature method:

  1. Given – an alias for setup of this feature method (Spock has more fixture methods like setupSpec which are executed once before the first feature method).
  2. When – a description of the stimulus to our software under test; the execution phase.
  3. Then – a description of the expected response; the verification phase.

More blocks can be found in Spock documentation, which I recommend to you for review.

Groovy and jUnit Tests Together With Maven

When writing tests in jUnit and specifications in Spock, we want to execute them both to test our system. In maven, we use the gmavenplus and surefire plugins to do that. The gmavenplus plugin compiles all Groovy files it finds in src/test/groovy:














To set our Java version to 1.8, we write the following plugin configuration:










How Spock Handles Parameterized Specification Methods

The Spock testing framework uses data tables to define test data for a feature method. The nice thing here is that you can have multiple methods with their own test data, unlike jUnit tests which have to share test data. To define a data table, we use the Where block and it must be the last block of a method.

def "say hello"(){


def helloWorld = new HelloWorld()


def result = helloWorld.speak toWho


result == expectedResult


toWho | expectedResult

"Dr. Who" | "helloDr. Who"

"world" | "hello world"


In this example, I introduced a minor bug in the test data to demonstrate how Spock shows failing tests:

Spock testing framework - Hello World
Above, we see a failing test and the failing assertion stacktrace, which raises a number of interesting points:

  • The printout of Spock assertion errors are a lot better than jUnit or hamcrest .
  • We do not see the result of the second data sample.

To fix this issue, we use the Spock.lang.Unroll annotation. This enables the visibility of each iteration as a separate feature. If we use this together with test data in the method name, then we receive a clear overview of what happened.


def "say hello #toWho"(){... 

Spock Testing Framework - Finished

Summary of the Spock Testing Framework

It’s clear that jUnit is an excellent test framework for all kind of Java tests, but there are some test scenarios where it’s not a good fit. For tests with test data, the jUnit Parameterized solution has some disadvantages that can be solved by using Spock, a Groovy-based test framework. You can use multiple specification methods and each of them is directly connected to their own test data. It’s possible to use jUnit and Spock tests together, so you should consider this option as part of your overall testing strategy.

Do you know of better ways to write tests with test data? If so, please leave a comment on this blog and we can discuss further. And for more information on development projects at Smaato, check out our development blog.

Do you want to regularly receive personalized newsletters on mobile advertising products, events, and related services from Smaato? Subscribe today!

Get our personalized newsletter on mobile advertising products, events, and related services.Subscribe

Related Articles