Programming for fun and profit

Programming tutorials, problems, solutions. Always with code.

Spock Framework Maven Gradle Setup

In this post we show common Spock Framework Maven/Gradle configurations ready to use in your projects and in the rest of the Spock Tutorial.

Libraries and tools we use here

  1. Spock Framework 1.0
  2. Hamcrest 1.3 (JUnit test matchers that work in Spock too)
  3. CGBLIB 3.1 (to mock classes)
  4. Objenesis 2.1 (to mock classes without default constructors)
  5. Gradle 2.14
  6. Maven 3.3

Sample Spock test

First, lets create a test that fails, so we can verify that our build tool picks it up for execution. Directory for tests:

mkdir -p src/test/groovy/com/farenda/tutorials/spock

In the above directory create a file HelloSpockTest.groovy with the code:

package com.farenda.tutorials.spock

import spock.lang.Specification

class HelloSpockTest extends Specification {

    def 'should test something useful'() {

Spock Framework Gradle configuration

Gradle buildscript

Here’s a Gradle buildscript that allows to run Spock Framework tests:

buildscript {
    repositories {

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

jar {
    baseName = 'your-project-name'
    version =  '0.1.0'

repositories {
    // Spock releases are available from Maven Central

// Set Java version here, else default will be used:
sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    // Spock Framework basic dependencies:
    testCompile "org.codehaus.groovy:groovy-all:2.4.1"
    // The version have to be compatible with Groovy:
    testCompile "org.spockframework:spock-core:1.0-groovy-2.4"

    // Optional dependencies Spock
    // To use Hamcrest matchers:
    testCompile "org.hamcrest:hamcrest-core:1.3"

    // To mock classes:
    testRuntime "cglib:cglib-nodep:3.1"
    // Use with cglib to mock classes without default constructor:
    testRuntime "org.objenesis:objenesis:2.1"

task wrapper(type: Wrapper) {
    gradleVersion = '2.14'

Gradle setup verification

Now, lets run the test from the command line to verify Gradle setup:

$> gradle test
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE

com.farenda.tutorials.spock.HelloSpockTest > should test something useful FAILED
    org.spockframework.runtime.ConditionNotSatisfiedError at HelloSpockTest.groovy:9

1 test completed, 1 failed
:test FAILED

FAILURE: Build failed with an exception.

Test has been executed and failed as expected! :-)

Spock Framework Maven configuration

Now it’s time for Maven configuration. It’s more verbose

Maven POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""



        <!-- Spock Framework basic dependencies: -->
            <!-- The version have to be compatible with Groovy -->
        <!-- To use Hamcrest matchers: -->
        <!-- To mock classes: -->
        <!-- Use with cglib to mock classes without default constructor: -->

            <!-- GMavenPlus plugin -->
            <!-- Enable if you use *Spec.groovy naming for your tests: -->
            <!-- <plugin> -->
            <!--    <artifactId>maven-surefire-plugin</artifactId> -->
            <!--    <configuration> -->
            <!--       <includes> -->
            <!--           <include>**/*Test.class</include> -->
            <!--           <include>**/*Spec.class</include> -->
            <!--       </includes> -->
            <!--    </configuration> -->
            <!-- </plugin> -->

Well, Maven setup is very verbose, but at least it is very well supported by many IDEs. One thing to keep in mind is that if tests are named with Test.groovy suffix then they will be picked up automagically, but if you use Spec.groovy suffix then you have to configure that in maven-surefire-plugin as you can see above (commented out part).

Maven setup verification

Lets check whether Maven is actually compiling and running Spock Framework tests:

$> mvn test
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building javaproject 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[... cut for brevity ...]
[INFO] --- gmavenplus-plugin:1.5:testCompile (default) @ javaproject ---
[INFO] Using Groovy 2.4.1 to perform testCompile.
[INFO] Compiled 1 file.
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ javaproject ---
[INFO] Surefire report directory: /home/java/spock-gradle-maven-setup/target/surefire-reports

 T E S T S
Running com.farenda.tutorials.spock.HelloSpockTest
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.664 sec <<< FAILURE!
should test something useful(com.farenda.tutorials.spock.HelloSpockTest)  Time elapsed: 0.127 sec  <<< FAILURE!
Condition not satisfied:


      at com.farenda.tutorials.spock.HelloSpockTest.should test something useful(HelloSpockTest.groovy:9)

Results :

Failed tests:   should test something useful(com.farenda.tutorials.spock.HelloSpockTest): Condition not satisfied:(..)

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------

It “works”! :-) The most important part here is GMavenPlus plugin that runs Groovy compiler, hence makes the tests runnable.


Gradle and Maven have are the most popular build tools. Spock Framework configuration in Gradle is simpler due to its Groovy nature.

Anyway, the tests are picked up and now it’s your job to make them pass. ;-)

Proceed to the Spock Framework Tutorial to learn more!

Share with the World!