Hi,
I would like to use dependency injections in my test code. For example, I need a fake session to test the app and I want to build a fake JWT using my TokenFactory.
public class UserControllerTest extends WithApplication
{
@Inject
private TokenFactory tokenFactory;
@Test
public void canCreateUser()
{
Token token = tokenFactory.build(ImmutableMap.of(
"userId", "1"
));
Call call = routes.UserController.submit();
Result result = route(fakeRequest(call)
.session(ImmutableMap.of(
"name", "manager",
"token", token.get()
))
.bodyForm(ImmutableMap.of(
"name", "John"
)));
assertEquals(OK, result.status());
}
}
But the tokenFactory
throws a NullpointerException.
I tried with a GuiceInjectorBuilder
:
private TokenFactory tokenFactory = new GuiceInjectorBuilder()
.overrides(bind(TokenFactory.class).toInstance(new TokenFactory()))
.build()
.instanceOf(TokenFactory.class);
But the Configuration instance injected inside TokenFactory is not properly injected causing an other NullpointerException.
How to make dependency injection fully work with tests?