Grails and tests - Part I: Getting started with TDD
Published by peter September 26th, 2007 in grails, groovy, testing.Writing tests for a Grails applications is really simple. In Groovy code actually compiles when non-existing methods of objects are referenced (as opposed to Java) which makes it possible exercise TDD (test driven development).
Grails supports two types of tests out of the box, unittests and integrationtests. Tests are located in the test folder of a generated Grails project.
Lets start with a simple integration test (I'll feature webtests and the like in a follow-up to this post) for a domain object.
First, create a domain class using the "grails create-domain-class" command. This will create a domain class AND the skeleton to write integration tests for the generated class (I choose to use the Battle class of the presentation I gave yesterday). The skeleton will look something like this:
Now, we want to test and see if we can store the expected fields in the battle object:
Running the test (grails test-app Battle) results, as expected in a failure:
-
Running 1 Integration Test...
-
Running test BattleTests...
-
testCRUD...FAILURE
After running the tests Grails creates a nice HTML overview of all the results as well:
In the image above you'll see the error message "No such property: name for class: Battle
". Adding the property to the Battle class fixes this and results in a successful test:
-
Running 1 Integration Test...
-
Running test BattleTests...
-
testCRUD...SUCCESS
Apart from the default assertion methods inherited from the JUnit framework's TestCase class, GroovyTestCase also offers a couple of other assertions handing us quite a useful/complete set of tools.





















2 Responses to “Grails and tests - Part I: Getting started with TDD”
Please Wait
Leave a Reply