Login required
Download

Spock Plugin - spockframework.org

(3)
Used by approximately
5%
of Grails users
Author(s) Spock Framework
Current Release 0.5-groovy-1.7   (1 year ago)
Grails Version 2.0.0.M1 > *
Tags testing 
Dependency
compile ":spock:0.5-groovy-1.7"
Test your Grails apps with Spock
Last updated by nwwells 1 month ago
Grails 1.2:
grails install-plugin spock 0.5-groovy-1.6
Grails 1.3:
grails install-plugin spock 0.5-groovy-1.7
Grails 2.0:
grails install-plugin spock 0.6-SNAPSHOT
Last updated by nwwells 1 month ago
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

For Grails 1.2: "grails install-plugin spock 0.5-groovy-1.6"

For Grails 1.3: "grails install-plugin spock 0.5-groovy-1.7"

For Grails 2.0: "grails install-plugin spock 0.6-SNAPSHOT"

Appetizer

Here is a very simple Grails unit specification in Spock:

import grails.plugin.spock.*

class MySpec extends UnitSpec { def "domain mocking"() { setup: mockDomain(Person)

when: new Person(name: name).save()

then: Person.findByName(name) != null

where: name = "bill" } }

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 the Grails 1.2 test type targeting feature 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.

Base Specifications

Several grails specific specification super classes are also provided to make testing your Grails app even easier. These classes for the most part mirror the existing Grails classes such as GrailsUnitTestCase .

All of these classes are in the grails.plugin.spock package.

UnitSpec

The UnitSpec class mirrors the GrailsUnitTestCase class. It features all of the mock* methods that this class has such as mockDomain , mockForConstraintsTests etc.

The example at the start of this page is an example of using UnitSpec .

ControllerSpec

The ControllerSpec class mirrors the ControllerUnitTestCase class. It is a subclass of UnitSpec (so is designed for unit testing) that tests a controller based on the class name. For example, MyControllerSpec will test MyController .

The controller:

class MyController {
  def text = {
    render "text"
  }

def someRedirect = { redirect(action: "someRedirect") }

def bodyElementText = { render request.'XML'.body.text() }

def model = { [a: '1'] } }

The specification:

import grails.plugin.spock.*

class MyControllerSpec extends ControllerSpec {

def 'text action'() { when: controller.text()

then: mockResponse.contentAsString == "text" }

def 'some redirect action'() { when: controller.someRedirect()

then: redirectArgs == [action: "someRedirect"] }

def 'bodyElementText action'() { when: xmlRequestContent = requestContent

and: controller.bodyElementText()

then: mockResponse.contentAsString == value

where: value << ['value', 'value'] requestContent << ["<request><body>value</body></request>", { request { body('value') } }] }

def 'model action'() { expect: controller.model() == [a: '1'] } }

TagLibSpec

The TagLibSpec class is suitable for testing tag libs. It is a subclass of UnitSpec (so is designed for unit testing) that tests a tag lib based on the class name. For example, MyTagLibSpec will test MyTagLib .

The tag lib:

class MyTagLib {
  def bar = { attrs, body ->
    out << "<p>Hello World!</p>"
  }

def bodyTag = { attrs, body -> out << "<${attrs.name}>" out << body() out << "</${attrs.name}>" } }

import grails.plugin.spock.*

class MyTagLibSpec extends TagLibSpec {

def 'bar tag'() { expect: bar() == "<p>Hello World!</p>" }

def 'bar tag subsequent call'() { expect: bar() == "<p>Hello World!</p>" bar() == "<p>Hello World!</p>" }

def 'body tag'() { expect: bodyTag(name: 'a') { "Foo" } == "<a>Foo</a>" bodyTag(name: 'b') { "Bar" } == "<b>Bar</b>" } }

IntegrationSpec

The IntegrationSpec class is the base class for integration tests (i.e. test/integration ). It supports autowiring of beans in the application context…

import grails.plugin.spock.IntegrationSpec

class MyIntegrationSpec extends IntegrationSpec { def myService // will be autowired

/* Spock tests */ }

Like regular Grails integration tests, all tests run in a database transaction that is rolled back. You can disable this behaviour by setting transactional to false…

import grails.plugin.spock.IntegrationSpec

class MyIntegrationSpec extends IntegrationSpec { static transactional = false

/* Spock tests */ }

GroovyPagesSpec

The GroovyPagesSpec is a kind of integration test that is useful for integration test GSP code.

For example:

import grails.plugin.spock.*

class FormatBooleanTagSpec extends GroovyPagesSpec {

def "simple format boolean usage"() { when: template = '<g:formatBoolean boolean="${flag}" true="T" false="F" />' params = [flag: flag] body = null

then: output == letter

where: flag << [true, false] letter << ['T', 'F'] } }

The pattern is always to set template , and optionally params and/or body in the when clause, and then check the output in the then clause.

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.

Last updated by admin 2 years ago
Last updated by admin 2 years ago