Depencency Injection in tests


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
    private TokenFactory tokenFactory;

    public void canCreateUser()
        Token token =
            "userId", "1"

        Call call = routes.UserController.submit();
        Result result = route(fakeRequest(call)
                "name", "manager",
                "token", token.get()
                "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()))

But the Configuration instance injected inside TokenFactory is not properly injected causing an other NullpointerException.

How to make dependency injection fully work with tests?