5

I have a controller with actions using the isLoggedIn() function provided by the Grails Spring Security Core plugin. I want to be able to unit test these actions, and I therefore need a way to mock the isLoggedIn() function so that it always returns false. This method is provided by a trait, which I believe is the source of my issue.

I have already tried to add a new function to the metaclass:

UserController.metaClass.isLoggedIn = { -> false}

This does not seem to be working, however; the original method is still being called.

Any suggestions as to how this can be accomplished?

Lagostra
  • 63
  • 1
  • 5

1 Answers1

5

You need to define the getter as:

UserController.metaClass.getIsLoggedIn = { -> false }

As an example, the following code:

class UserController { 
  def isLoggedIn = "original"
}


UserController.metaClass.getIsLoggedIn = { -> "get" }
UserController.metaClass.isLoggedIn    = { -> "direct" }


def u = new UserController()
println u.isLoggedIn

prints:

get
Matias Bjarland
  • 4,124
  • 1
  • 13
  • 21
  • This solution worked after using `loggedIn` instead of `isLoggedIn()`, and overriding `getLoggedIn()`. – Lagostra Feb 17 '17 at 12:29