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]
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
File | Command |
---|---|
test/unit/mypackage/PersonServiceListSpec.groovy | create-unit-test-service-list |
test/unit/mypackage/PersonServiceListMaxSpec.groovy | create-unit-test-service-list-max |
test/unit/mypackage/PersonServiceListOffsetSpec.groovy | create-unit-test-service-list-offset |
test/unit/mypackage/PersonServiceListSortOrderSpec.groovy | create-unit-test-service-list-sort-order |
test/unit/mypackage/PersonServiceCreateSpec.groovy | create-unit-test-service-create |
test/unit/mypackage/PersonServiceUpdateSpec.groovy | create-unit-test-service-update |
test/unit/mypackage/PersonServiceGetSpec.groovy | create-unit-test-service-get |
test/unit/mypackage/PersonServiceDeleteSpec.groovy | create-unit-test-service-delete |
test/unit/mypackage/PersonServiceCreateSpec.groovy
file, we will find something like this:
package mypackageimport 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 mypackageclass PersonMock { static Person mock( id ) { def instance = new Person( name:'A' * 1, lastName:'A' * 1, birthdate:new Date(), enabled:true, ) instance }}
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.