I spent a while confused today, when some fairly straightforward unit tests were running fine locally, but failing on our continuous integration server, jenkins.
The error was occurring in the setUp method, annotated
@Before, where we were setting a flash variable prior to running tests.
java.lang.NullPointerException: Cannot invoke method getFlashScope() on null object at grails.test.mixin.web.ControllerUnitTestMixin.getFlash(ControllerUnitTestMixin.groovy:159)
After much head scratching I came across this link
which explains a similar problem.
It appears that because the Controller uses a Mixin, it effectively extends
ControllerUnitTestMixin which is not abstract and also has a method annotated with
@Before. Junit does not guarantee the order in which the two methods annotated with @Before will be run (See http://junit.sourceforge.net/doc/faq/faq.htm#tests_2)
If the Mixin method does not run before my setUp method, the flash scope has not been mocked, hence the failure.
The solution suggested in the post is to call
super.bindGrailsWebRequest() in the setUp method. In our case it was simple enough to set the flash variable within the individual tests, instead of the setUp method.