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 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 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.

Running Tests

Tests are run just like normal Grails tests via grails 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 extend spock.lang.Specification in your spock-grails unit tests, just like regular Spock tests.

Integration Testing with Grails 2.0

Integration testing in Grails 2.0 remains unchanged from how you used Spock for integration tests in Grails 1.3 and earlier.

Testing pre Grails 2.0 apps

Functional Testing

The Spock plugin does not ship with any direct support for functional testing. However, the Geb plugin for Grails is compatible with the Spock plugin and allows you to write functional tests in an extremely expressive DSL.