Test Templates
Dependency :
compile ":test-template:1.3.2"
Summary
Description
Test Template Plugin
Please create issues in JIRA if you encounter problems.
To install:
The test template plugin requires the Grails Testing Plugin, so must install that plugin first
grails install-plugin testing grails install-plugin test-template
To run:
Say you're working on a new project:grails create-app bookstore cd bookstore grails install-plugin testing grails install-plugin test-template grails create-domain-class Book
grails generate-all Book -- OR -- grails generate-controller Book
For Grails version 1.0.3 you must manually generate the controller unit test by running the following command:grails generate-controller-unit-test Book
Overriding the plugin provided test templates:
grails install-test-templates
Experimental: Stub out a unit test for a Domain class
As pointed out in the docs for the Grails Testing Plugin, constraints often contain a lot of logic for your application and are rarely tested. To help out with that, the Test Template plugin provides a script that will create a stub of a unit test for you for a given domain class.For example, say you had the following domain class:class Book {
String title
String subTitle
Date publishedDate
static constraints = {
title(nullable:false, blank:false, size:1..100)
publishedDate(nullable:true)
}
}grails generate-domain-unit-test Book
- in the setup method, fill in all the properties that would make your domain class validate
- in each test method, write test code that will test your constraints. The plugin provides comments indicating which constraints are applied to a given property, you just need to write the test code.
import grails.test.GrailsUnitTestCase class BookUnitTests extends GrailsUnitTestCase { Book book void setUp() { super.setUp() // in testing-plugin 0.4 you can just do "mockForConstraintsTests(Book)" instead of these two lines registerMetaClass(Book) grails.test.MockUtils.prepareForConstraintsTests(Book) // TODO - fill out this book instance so it validates book = new Book() assertTrue "setup method: book should validate", book.validate() } void testTitleConstriants() { //test org.codehaus.groovy.grails.validation.NullableConstraint@de1237[false] //test org.codehaus.groovy.grails.validation.BlankConstraint@7bc5fd[false] //test org.codehaus.groovy.grails.validation.SizeConstraint@36f09[1..100] } void testPublishedDateConstriants() { //test org.codehaus.groovy.grails.validation.NullableConstraint@6ec9d4[true] } void testSubTitleConstriants() { //test org.codehaus.groovy.grails.validation.NullableConstraint@849937[false] } }
grails generate-domain-unit-test "*"