Spock Plugin - spockframework.org
15% of Grails users
Dependency :
test ":spock:0.7"
Summary
Test your Grails apps with Spock
Installation
Grails 2.0/2.1
By default, the Spock plugin configures itself for Groovy 1.8 which is used by Grails 2.0/2.1. So to use the Spock plugin with Grails 2.0/2.1, modify your BuildConfig.groovy file to include the following:grails.project.dependency.resolution = {
repositories {
grailsCentral()
mavenCentral()
}
plugins {
test ":spock:0.7"
}
}Grails 2.2
Grails 2.2 uses Groovy 2.0, which requires a special Spock version. So to use the Spock plugin with Grails 2.2, modify you BuildConfig.groovy file to include the following:grails.project.dependency.resolution = {
repositories {
grailsCentral()
mavenCentral()
}
dependencies {
test "org.spockframework:spock-grails-support:0.7-groovy-2.0"
}
plugins {
test(":spock:0.7") {
exclude "spock-grails-support"
}
}
}Grails 1.3
Grails 1.3 uses Groovy 1.7 which is not compatible with Spock 0.7. You can use Spock 0.6, but you'll need some extra config. So to use the Spock plugin with Grails 1.3, modify you BuildConfig.groovy file to include the following:grails.project.dependency.resolution = {
repositories {
grailsCentral()
mavenCentral()
}
dependencies {
test "org.spockframework:spock-grails-support:0.6-groovy-1.7"
}
plugins {
test(":spock:0.6") {
exclude "spock-grails-support"
}
}
}Grails 1.2:
The last version of Spock to support Groovy 1.6 (which is used by Grails 1.2.x) was 0.5. To use Spock with Grails 1.2.x, run the following from the command line:grails install-plugin spock 0.5-groovy-1.6
Description
Spock is a testing and specification framework for Java and Groovy applications. What makes it stand out from the crowd is its beautiful and highly expressive specification language. This plugin brings the power of Spock to Grails.Before using this plugin, head on over to the Spock homepage and get familiar with Spock.
If you have any questions or suggestions, please post to the Spock discussion forum (or its Nabble mirror). You can also raise an issue in the issue tracker. Sources for the Spock plugin can be found on Google code
Installation
See the Installation tab.If you are upgrading to Grails 2.2, then definitely look at the Installation tab for info on how to get the plugin working.
Appetizer
Here is a very simple Grails unit specification in Spock:import grails.test.mixin.*@TestFor(PostController) class PostControllerSpec extends spock.lang.Specification { def "Index action should redirect to list page"() { when: controller.index() then: response.redirectedUrl == "/post/list" } … }
Running Tests
Tests are run just like normal Grails tests viagrails test-app . A new "spock" test type is added to the unit, integration and functional phases. You can use test type targeting to run only your Spock tests:grails test-app :spock
Class names of Spock tests must end in either "Spec" or "Specification". Otherwise, the Grails test runner won't find them.
Unit Testing with Grails 2.0
Grails 2 introduced brand new unit test support based on mixins instead of testing super classes. These mixins, are fully compatible with Spock! This means that you use exactly the same approach as outlined in the Grails User Guide on Unit Testing. You should always extendspock.lang.Specification in your spock-grails unit tests, just like regular Spock tests.