November 16, 2008 – 7:46 pm
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 now 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)
}
}
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. It’s not completely magic – 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() {
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]
}
}
This is helpful in reminding you what you actually need to test – for example, did you remember that by not defining a constraint for the ’subTitle’ property, grails makes it a non-nullable field?
By the way, the script 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.
I’m thinking future versions of the plugin could go even further by offering:
- A ‘verbose’ template that would create multiple test methods for each possible test scenario for a given constraint, for instance a ’size:5..100′ constraint could generate test methods for:
- a string with a size of 4 (e.g. less than the minimum)
- a string with a size of 101 (e.g. more than the maximum)
- a string with a size of 50 (e.g. within the bounds of the constraint)
- Actually write the test code. Not sure if I’m totally warmed up to this idea yet, but it may be worth looking into. Contributions welcome, of course
Give it a shot. You can install the plugin by running the following commands:
grails install-plugin testing
grails install-plugin test-template
Posted in grails, plugin, testing | Comments Off