(Quick Reference)

6 Creating unit tests for services - Reference Documentation

Authors: Alejandro GarcĂ­a Granados

Version: 0.4

6 Creating unit tests for services

When you create a service class with the create-service-class command, it is highly recommended that you also create the corresponding unit tests. You can achieve this by executing the create-unit-test-service command:
grails create-unit-test-service [domainClass]
Suppose you have a class named mypackage.Person. With the create-unit-test-service command, you will get the following unit test files:
  • test/unit/mypackage/PersonServiceListSpec.groovy
  • test/unit/mypackage/PersonServiceListMaxSpec.groovy
  • test/unit/mypackage/PersonServiceListOffsetSpec.groovy
  • test/unit/mypackage/PersonServiceListSortOrderSpec.groovy
  • test/unit/mypackage/PersonServiceCreateSpec.groovy
  • test/unit/mypackage/PersonServiceUpdateSpec.groovy
  • test/unit/mypackage/PersonServiceGetSpec.groovy
  • test/unit/mypackage/PersonServiceDeleteSpec.groovy

You can generate each file with the following commands:

FileCommand
test/unit/mypackage/PersonServiceListSpec.groovycreate-unit-test-service-list
test/unit/mypackage/PersonServiceListMaxSpec.groovycreate-unit-test-service-list-max
test/unit/mypackage/PersonServiceListOffsetSpec.groovycreate-unit-test-service-list-offset
test/unit/mypackage/PersonServiceListSortOrderSpec.groovycreate-unit-test-service-list-sort-order
test/unit/mypackage/PersonServiceCreateSpec.groovycreate-unit-test-service-create
test/unit/mypackage/PersonServiceUpdateSpec.groovycreate-unit-test-service-update
test/unit/mypackage/PersonServiceGetSpec.groovycreate-unit-test-service-get
test/unit/mypackage/PersonServiceDeleteSpec.groovycreate-unit-test-service-delete
If we open the test/unit/mypackage/PersonServiceCreateSpec.groovy file, we will find something like this:
package mypackage

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

@TestFor(PersonService) @Mock(Person) class PersonServiceCreateSpec extends Specification {

def "test ok"() {

when: def instance = PersonMock.mock( 0 ) service.create( instance ) then: Person.count() == 1

}

def "test Person null"() {

when: def instance = null service.create( instance ) then: IllegalArgumentException e = thrown() e.message == "Parameter 'person' is null"

}

def "test Person invalid"() {

when: def instance = PersonMock.mock( 0 ) instance.name = name service.create( instance ) then: IllegalArgumentException e = thrown() e.message == "Parameter 'person' is invalid" where: name = null

}

}

For versions of Grails prior to 2.3.0, you need to install the Grails Spock Plugin if you want to execute the tests.
You can notice the presence of the PersonMock class. This class is automatically generated and placed in the src/groovy/mypackage/PersonMock.groovy file, and it contains utility methods for mocking a Person valid domain class:
package mypackage

class PersonMock {

static Person mock( id ) {

def instance = new Person( name:'A' * 1, lastName:'A' * 1, birthdate:new Date(), enabled:true, ) instance

}

}

If you change the id of this class (to assigned, for example), this class already consider it, allowing you to set the id by hand.

You can generate the mock class separately by executing the create-mock command.