Getting started with spock

Download spock eBook

Remarks

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.

Versions

VersionRelease Date
1.1 rc 32016-11-03
1.02016-03-02

"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"() {
        when:
            def newMessage = message
        then:   
            newMessage == 'Hello world!'
    }

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

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

@Grab('org.spockframework:spock-core:1.1-groovy-2.4.1')
 
  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.

Stats

42 Contributors: 2
Friday, January 13, 2017
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook