Mock Grails Spring Security Logged in User

Viewed 3446

Looking for a way to mock spring security in some unit/integration tests.

  • Grails: V2.1.0
  • Spring Security Core: V1.2.7.3

Controller has the following:

// some action
def index(){
   def user = getLoggedInUser()
   render ....
}
...

private getLoggedInUser(){
    return User.get(springSecurityService.principal.id)
}

I tried the following and various other ways but can't see to get it to work:

void testSomething(){
    def dc = new SomeController()
    dc.springSecurityService = [
            encodePassword: 'password',
            reauthenticate: { String u -> true},
            loggedIn: true,
            principal: [username:"Bob"]]
    dc.index() 
    ... assertion....

It seems that the user is not getting created and can't get the principal.id. Any suggestions or better alternatives?

1 Answers
Related