Java Programming Tutorials

Java programming tutorials with many code examples!

Spock Framework Maven Gradle Setup


How to configure project to run Spock Framework tests in Maven or Gradle? The following examples allow to start with Spock Framework in no time!


Libraries used in this tutorial:

  1. Spock Framework 1.0
  2. Hamcrest 1.3 (test matchers)
  3. CGBLIB 3.1 (mocking classes)
  4. Objenesis 2.1 (mocking classes without default constructors)
  5. Gradle 2.4
  6. Maven 3.3

First, lets create sample test that fails:

package com.farenda.solved

import spock.lang.Specification

class HelloSpockTest extends Specification {

    def 'should test something useful'() {

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.4'

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.solved.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! :-)

Now it’s time for Maven configuration:

<?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 -->

Well, Maven setup is very verbose, but at least it is very well supported by many IDEs.

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.solved.HelloSpockTest
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.664 sec <<< FAILURE!
should test something useful(com.farenda.solved.HelloSpockTest)  Time elapsed: 0.127 sec  <<< FAILURE!
Condition not satisfied:


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

Results :

Failed tests:   should test something useful(com.farenda.solved.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 been configured to run Spock Framework tests. Now it’s your turn to make them pass. ;-)

Now you can proceed to the Spock Framework Tutorial to learn more!

Share with the World!