我有一个带有经过验证的路线的游戏应用程序。我实现了一个Authenticator,将用户存储到elasticsearch中。我控制器中的安全方法使用@Security.Authenticated注释进行注释。对于使用嘲笑的单元测试,我想模拟这个类,但是我不知道该怎么做。

我在Guice中使用DI。所以我尝试了这种方法:


开发一个AuthenticatorWrapper,如下所示:

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;
    }
}


此类具有Authenticator作为参数,应由Guice在应用启动时注入。
我开发了一个guice模块,定义了类Authenticator.classMyCustomAuthenticator.class的绑定
我的安全路线用@Security.Authenticated(AuthenticatorWrapper.class)注释


在我的测试中,我可以轻松提供类MyCustomAuthenticator的模拟物,从而创建模拟物,定义测试范围guice模块以及定义从Authenticator.class到模拟物的绑定。

我认为这应该可行,但事实并非如此。无论是在正常运行时还是从我的测试中,绑定似乎均不起作用。在以下情况下,我从包装器中获取了nullPointerException:参数不是Guice注入的。

所以我的问题是:


身份验证器是从Guice注入我的身份验证器的好方法吗?也许有一种更简便的方法将播放器Authenticator注入Guice的注释中?
Guice不将Authenticator注入我的包装器是否正常? [编辑->是,因为注释手动实例化了我的对象,并且不使用guice。我对吗?]
我可以通过直接在注释中设置Authenticator来简化我的应用程序,但是如何在测试中模拟该身份验证器?


谢谢 :)

最佳答案

找到了解决方法。我只使用了Play框架2.4提供的访问方法,因为它完全集成了Guice。这是我的身份验证包装程序类:

public class AuthenticatorWrapper extends Security.Authenticator {

    private final Security.Authenticator authenticator;

    public AuthenticatorWrapper() {
        authenticator = Play.application().injector().instanceOf(Security.Authenticator.class);
    }

    @Override
    public String getUsername(Http.Context ctx) {
        return authenticator.getUsername(ctx);
    }

    @Override
    public Result onUnauthorized(Http.Context ctx) {
        return authenticator.onUnauthorized(ctx);
    }
}


我只是使用Play.application()。injector()访问器来获取Guice提供的Security.Authenticator实例。因此,在我的application.conf中,我仅配置了一个Guice模块,该模块将Security.Authenticator绑定到所需的实现。

10-07 23:39