Unit testing a class that uses SecurityContext

So I have a class (very much simplified for this post) that has some Groovy code that looks like this:

if (SecurityUtils.securityContext?.authenticated){
return true
} else {
return false
}

I'd like to unit test that chunk to make sure everything's working the way I expect it to. In my unit test, I don't have access to the DBRealm and all the other goodies that I'd normally have (otherwise I'd just log in a user and then run the test). Is there a way I can "mock" out the SecurityUtils to think that I have an authenticated user? Or somehow fake it out into thinking a user is authenticated?

Thanks,

Mike

Re: Unit testing a class that uses SecurityContext

Hi Mike,

SecurityUtils.getSecurityContext really is just this:

public static SecurityContext getSecurityContext() {
    return ThreadContext.getSecurityContext();
}

Therefore, to mock a SecurityContext in your test cases, just call ThreadContext.setSecurityContext(aSecurityContext); right before that method is invoked, and it will use your mock object ;)

Cheers,

Les