(Quick Reference)

9 Creating unit tests for controllers - Reference Documentation

Authors: Alejandro GarcĂ­a Granados

Version: 0.4

9 Creating unit tests for controllers

When you create a controller class with the create-controller-class command, it is highly recommended that you also create the corresponding unit tests. You can achieve this by executing the create-unit-test-controller command:
grails create-unit-test-controller [domainClass]
Suppose you have a class named mypackage.Person. With the create-unit-test-controller command, you will get the following unit test files:
  • test/unit/mypackage/PersonControllerIndexSpec.groovy
  • test/unit/mypackage/PersonControllerContentSpec.groovy
  • test/unit/mypackage/PersonControllerListSpec.groovy
  • test/unit/mypackage/PersonControllerCreateSpec.groovy
  • test/unit/mypackage/PersonControllerSaveSpec.groovy
  • test/unit/mypackage/PersonControllerEditSpec.groovy
  • test/unit/mypackage/PersonControllerUpdateSpec.groovy
  • test/unit/mypackage/PersonControllerDeleteSpec.groovy

You can generate each file with the following commands:

FileCommand
test/unit/mypackage/PersonControllerIndexSpec.groovycreate-unit-test-controller-index
test/unit/mypackage/PersonControllerContentSpec.groovycreate-unit-test-controller-content
test/unit/mypackage/PersonControllerListSpec.groovycreate-unit-test-controller-list
test/unit/mypackage/PersonControllerCreateSpec.groovycreate-unit-test-controller-create
test/unit/mypackage/PersonControllerSaveSpec.groovycreate-unit-test-controller-save
test/unit/mypackage/PersonControllerEditSpec.groovycreate-unit-test-controller-edit
test/unit/mypackage/PersonControllerUpdateSpec.groovycreate-unit-test-controller-update
test/unit/mypackage/PersonControllerDeleteSpec.groovycreate-unit-test-controller-delete
If we open the test/unit/mypackage/PersonControllerSaveSpec.groovy file, we will find something like this:
package mypackage

import grails.test.GrailsMock import grails.test.mixin.* import spock.lang.*

@TestFor(PersonController) @Mock(Person) class PersonControllerSaveSpec extends Specification {

def setup() { views[ '/person/_form.gsp' ] = getTemplate() }

def "test ok"() {

when: def control = mockPersonService() request.method = 'POST' setUpParams() controller.save() control.verify() then: flash.formMessage == 'default.created.message' response.redirectedUrl == "/person/edit/1" response.status == 302

}

def "test params invalid"() {

when: def control = mockPersonService( false ) request.method = 'POST' setUpParams() params.name = null controller.save() control.verify() then: response.text == 'OK' response.status == 400

}

@Ignore( 'See http://jira.grails.org/browse/GRAILS-8426' ) def "test request method invalid"() {

when: request.method = 'GET' controller.save() then: response.status == 405

}

private String getTemplate() { '<g:if test="${personInstance}">OK</g:if><g:else>ERROR</g:else>' }

private GrailsMock mockPersonService( save = true ) {

def control = mockFor( PersonService ) control.demand.create( 1 ) { Person instance -> if ( save ) { instance.id = 1 instance.save( failOnError:true ) } else throw new IllegalArgumentException( 'error' ) } controller.personService = control.createMock() control

}

private void setUpParams() {

def mock = PersonMock.mock( 0 ) mock.properties.each{ params."${it.key}" = it.value }

} }

For versions of Grails prior to 2.3.0, you need to install the Grails Spock Plugin if you want to execute the tests.

The unit tests make use of the Grails Testing API. We strongly recommend you to read the Grails Testing chapter for a further reference.