Java Programming Tutorials

Java programming tutorials with many code examples!

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'() {
        expect:
        false
    }
}

Spock Framework Gradle configuration

Gradle buildscript

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

buildscript {
    repositories {
        mavenCentral()
    }
}

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

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

repositories {
    // Spock releases are available from Maven Central
    mavenCentral()
}

// 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
:compileTestGroovy
:processTestResources UP-TO-DATE
:testClasses
:test

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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.farenda.tutorials</groupId>
    <artifactId>spock</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- Spock Framework basic dependencies: -->
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.4.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-core</artifactId>
            <!-- The version have to be compatible with Groovy -->
            <version>1.0-groovy-2.4</version>
            <scope>test</scope>
        </dependency>
        <!-- To use Hamcrest matchers: -->
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
        <!-- To mock classes: -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>3.1</version>
            <scope>test</scope>
        </dependency>
        <!-- Use with cglib to mock classes without default constructor: -->
        <dependency>
            <groupId>org.objenesis</groupId>
            <artifactId>objenesis</artifactId>
            <version>2.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- GMavenPlus plugin -->
            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </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> -->
        </plugins>
    </build>
</project>

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] ------------------------------------------------------------------------
[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]
[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:

false

      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] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

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

Summary

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!