How to mock/stub calls to message taglib in Grails controller
        Posted  
        
            by Dave
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Dave
        
        
        
        Published on 2010-01-06T15:01:44Z
        Indexed on 
            2010/04/23
            21:23 UTC
        
        
        Read the original article
        Hit count: 483
        
I've got a Grails controller which relies on the message taglib to resolve an i18n message:
class TokenController {
def passwordReset = {
    def token = DatedToken.findById(params.id);
    if (!isValidToken(token, params)) {
        flash.message = message(code: "forgotPassword.reset.invalidToken")
        redirect controller: 'forgotPassword', action: 'index'
        return
    }
    render view:'/forgotPassword/reset', model: [token: token.token]
}
}
I've written a unit test for the controller:
class TokenControllerTests extends ControllerUnitTestCase {
void testPasswordResetInvalidTokenRedirect() {
    controller.passwordReset()
    assert...
}
}
Since the message taglib is called in the controller I get a MissingMethodException:
groovy.lang.MissingMethodException: No signature of method: TokenController.message() is applicable for argument types: (java.util.LinkedHashMap) values: [[code:forgotPassword.reset.invalidToken]]
Does anyone know the best way to get around this issue in a unit test? Ideally I would like to perform assertions on the message but right now I'd be happy if the test just ran!
Thanks
© Stack Overflow or respective owner