Spock Plugin - spockframework.org

  • Authors : Spock Framework
17 votes

15% of Grails users

Dependency :
test ":spock:0.7"

Documentation

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.

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" } … }

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

For examples of different kinds of Grails tests for Grails 1.3.x and earlier, please visit the dedicated Grails Spock Examples Project.

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.

Help & Community

Mailing Lists

Consider joining the Spock mailing list to join the Spock community.

Grails specific Spock issues can be raised on that list, or the normal Grails user list.

Reporting Issues

Any issues encountered with either Spock, or this plugin, can be raised in the Spock issue tracker.