Getting started with spock
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#
Version | Release Date |
---|---|
1.1 rc 3 | 2016-11-03 |
1.0 | 2016-03-02 |
Installation or Setup
Spock framework information can be found at the Spock website.
There are basically three ways to use Spock in Groovy
- 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')
- 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"
}
...
- 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"() {
when:
def newMessage = message
then:
newMessage == 'Hello world!'
}
def "The world can say hello using expect"(){
expect:
message == 'Hello world!'
}
}