ad-hockery: /ad·hok'@r·ee/, n.
Gratuitous assumptions... which lead to the appearance of semi-intelligent behavior but are in fact entirely arbitrary.
[Jargon File]
Grails controllers and tag libs can access any tag as though it were a method. The most common use for this is probably accessing i18n messages via the g:message tag. However, because tag access is magic wired up by Grails it's not available in unit tests without some effort.
This is a perennial nuisance. Not exactly difficult to solve and yet something I find myself solving over and over in different tests and different projects.
I've come up with what I think is a pretty good and re-usable solution. It allows you to specify messages if you want to, or just use the message code if it's not something you care about in a particular test. As an aside, at a unit test level, I think testing that the correct message code is being used is probably the right thing to do; I'd leave testing actual message text to end-to-end tests.
Here's an example. Imagine we're testing the following controller that displays a simple greeting:
Here's a spec that shows the behaviour both when a message is specified and when it isn't:
A few things to note:
The stubbed message tag returns the code if there is no message defined
message arguments are ignored unless there is a message defined
Although in the example I've used Spock, this technique would work equally well with JUnit tests extending ControllerUnitTestCase. It will also work just as well for tag libs tests extending TagLibUnitTestCase or TagLibSpec.