ControllerUnitTestCase: Documentation Has Arrived
My team started on our first Grails app back in mid-August. I set out to provide good examples of using the new testing features in Grails 1.1 to encourage the rest of the team. Domain class testing went great, as did Service testing. Unfortunately, there wasn’t any documentation on the ControllerUnitTestCase class in the Grails User Guide, and I couldn’t find any blog or forum posts to help out. I tried creating an instance of it and tossing in a call to mockController(), but I didn’t get very far. I had to shift focus to other tasks before I could dive into the source code itself and figure out what it did, so progress stopped for a while.
Fast forward to today when I finally had a strong need for a controller unit test setup. I dove into the code and realized three things very quickly:
- You don’t need to call mockController(). It does that for you based on the name of the test class itself.
- You don’t want to call the constructor of your controller class in the test cases. Use the newInstance() method instead so that all the proper scaffolding is put in place.
- You don’t even have to call newInstance() most of the time because the setUp() method inits a controller variable for you already.
Doh! I wish I had taken 15 minutes back in August to look at the source. It would have saved quite a bit of time by encouraging us to get proper controller test cases in place faster.
To add insult to injury, after spending that time and more getting some test cases just right, I decided to try a search for more documentation online again. This time there was an answer to my query for help. A post to the grails-user mailing list less than a month ago generated several responses with useful info, including Scott Davis posting about the most recent addition to his Mastering Grails series which was published just days before the post.
So nine months after the release of Grails 1.1 there is now good documentation on this great piece. Many thanks to those who pulled it together.