Generated Controller Test Cases

November 16, 2008 – 9:04 am

The generated controller scaffolding from Grails is a great tool for learning the framework quickly. It provides examples of controllers, controller actions, simple GORM methods on your domain classes, Groovy Server Pages and tag libraries - everything you need to get started - EXCEPT for how to unit test the stuff.

I’ve always thought a good addition to Grails would be the ability to generate unit tests for all that scaffolding code in your generated controllers. With the advent of testing enhancements coming to Grails 1.1 and the Grails Testing Plugin making it much easier to unit test Grails artifacts, this is more of a reality.

So I buckled down and created a plugin with a template for testing generated controllers. If you’re on Grails 1.0.4, it hooks directly into events in the build system to create the unit test file automagically. If you haven’t upgraded yet and you’re still on Grails 1.0.3, there’s a script you can call to generate the unit test manually.

I decided to make the Testing Plugin a pre-requisite for the “Test Template” plugin, since it makes the test code SO much easier to write. So to get started, run “grails install-plugin testing“, followed by “grails install-plugin test-template“. Here’s an small example:

grails create-app bookstore
cd bookstore
grails install-plugin testing
grails install-plugin test-template
grails create-domain-class Book
grails generate-controller Book (or you could run generate-all Book)

If you have Grails 1.0.4, that last line will generate the controller for you AND also generate a unit test (test/unit/BookControllerUnitTests.groovy). The unit test will have test methods for each controller action, with several test methods for each of the actions that have if/else statements in them.

If you have Grails 1.0.3, you have to manually generate the unit test, which you can do with this line:

grails generate-controller-unit-test Book

Yes, that’s a long command name, but what are you doing still on Grails 1.0.3 anyway? Grails 1.0.4 has been out for like 48 hours - go upgrade already!

Now run your test suite (grails test-app) and watch your newly generated tests scroll by!

The plugin uses the createArtefact method in Init.groovy, so it also handles packages:

grails create-domain-class com.pirgaua.Book
grails generate-controller com.piragua.Book
// -> generates test/unit/com/piragua/BookControllerUnitTests.groovy

And, it also handles the new “uber-generate” feature in 1.0.4 (courtesy of Marcel Overdijk and GRAILS-2946:

grails create-domain-class Book
grails create-domain-class Author
grails create-domain-class Store
grails create-domain-class Cart
// edit your domain classes and add properties, constraints, etc...
grails generate-all "*"
// -> generates controllers, views, and controller unit tests for ALL your domain classes

And you can override the plugin provided template, just run “grails install-test-templates” and modify the template code to suit your own needs (the templates are copied to src/templates/artifacts/test-templates).

Speaking of the next version - the next feature I want to add to the plugin is a way to generate a unit test for testing domain constraints. Stay tuned!

More documentation on the Test Template plugin page at grails.org

Post a Comment