spock Getting started with spock


Spock is a testing and specification framework for Java and Groovy applications. It uses an expressive specification language.

A developer, usually a Java developer using groovy, may use Spock as specification driven checking (testing) framework. A tester may use Spock to write functional checks of software package allowing the full power of groovy.

It basically allows you to write specifications that describe expected features.


VersionRelease Date
1.1 rc 32016-11-03

Installation or Setup

Spock framework information can be found at the Spock website.

There are basically three ways to use Spock in Groovy

  1. as a dependency using the Grape dependency manager :

Add the following to your groovy script.

@Grab(group='org.spockframework', module='spock-core', version='1.1-groovy-2.4.1')

or in shorthand

  1. as a maven dependency using the Gradle build tool (build.gradle)

Add the following dependency to the build.gradle file under dependencies

dependencies {
    // mandatory dependencies for using Spock
    compile "org.codehaus.groovy:groovy-all:2.4.1"
    testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
  1. Adding the spock-core library to your lib path

Adding the spock-core-1.0-groovy-2.4.jar to a location in your classpath where groovy can find it.

and last but not least you need to import the library so that it can be used in your groovy script

import spock.lang.*

After you installed spock then try one of the hello world examples.

"Hello World" using when and then or expect

import spock.lang.*

class HelloWorldSpec extends Specification {

    @Shared message = 'Hello world!'

    def "The world can say hello using when and then"() {
            def newMessage = message
            newMessage == 'Hello world!'

    def "The world can say hello using expect"(){
            message == 'Hello world!'