I have a play application with authenticated routes. I implemented an Authenticator, storing users into elasticsearch. My securized methods in my controllers are annotated with the @Security.Authenticated
annotation. For my unit tests with mockito, I would like to mock this class but I don't know how to do this.
I am using DI with Guice. So I tried this approach:
Develop an AuthenticatorWrapper as following:
public class AuthenticatorWrapper extends Security.Authenticator { private Authenticator authenticator; @Override public String getUsername(Http.Context ctx) { return authenticator.getUsername(ctx); } @Override public Result onUnauthorized(Http.Context ctx) { return authenticator.onUnauthorized(ctx); } @Inject public void setAuthenticator(Authenticator authenticator) { this.authenticator = authenticator; } }
This class has an Authenticator as parameter, which is supposed to be injected by Guice when the app starts.
I developed a guice module defining a binding for class
Authenticator.class
toMyCustomAuthenticator.class
My securized route are annotated with
@Security.Authenticated(AuthenticatorWrapper.class)
In my test I can easily provide a mock of class MyCustomAuthenticator
my creating the mock, define test scope guice module, and defining a binding from Authenticator.class
to my mock.
I thought this should work but this is not the case. Both at normal runtime or from my tests, the binding seems not working. I have nullPointerException
from the wrapper when: the Authenticator
parameter is not injected by Guice.
So my questions are:
- Does the Authenticator is a good approach to Inject my authenticator from Guice? Maybe there is an easier way to inject a play Authenticator into annotations from Guice?
- Is it normal that Authenticator is not injected by Guice into my wrapper? [EDIT -> yes because the annotation manually instantiates my object and doesn't use guice. Am I right?]
- I can simplify my application by set directly
MyCustomAuthenticator
into the annotations, but how can I mock this authenticator in my tests?
Thanks :)