Plugins You can find out about all the publicly available Grails plugins.

Test Templates

  • Tags: testing
  • Latest: 1.3.2
  • Last Updated: 20 November 2008
  • Grails version: *
  • Authors: Mike Hugo
0 vote
compile "org.grails.plugins:test-template:1.3.2"




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

Now you put some properties into your Book domain class and generate scaffolded controller and views:

grails generate-all Book
-- OR --
grails generate-controller Book

Provided you're using Grails 1.0.4 and the test-template plugin is installed, the plugin will generate a controller unit test in ${baseDir}/test/unit/

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
This will install the template for the generated unit test into your project at ${baseDir}/src/templates/artifacts/test-templates. If a template is provided in this location, the plugin will use this template rather than the default provided by the plugin.

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)

With the plugin installed, type

grails generate-domain-unit-test Book

The plugin will generate and stub out a unit test for you in test/unit/BookUnitTests.groovy. You still need to do some work now:

  • 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.
Here's what a stubbed unit test looks like out of the box:
import grails.test.GrailsUnitTestCase
class BookUnitTests extends GrailsUnitTestCase {
    Book book 
    void setUp() {
        // in testing-plugin 0.4 you can just do "mockForConstraintsTests(Book)" instead of these two lines
	// TODO - fill out this book instance so it validates
	book = new Book()
        assertTrue "setup method: book should validate", 
	void testTitleConstriants() {		 
		//test [email protected][false]  
		//test [email protected][false]  
		//test [email protected][1..100] 
	void testPublishedDateConstriants() {		
		//test [email protected][true] 
	void testSubTitleConstriants() {		
		//test [email protected][false] 

By the way, this also works with the uber generate feature of Grails 1.0.4 - so if you have lots of domain classes and want to generate unit tests for all of them in one fell swoop, you can type:

grails generate-domain-unit-test "*"
and it will stub out unit tests for all your domain classes.