如何在带有Guice注入的play框架中模拟Authenticator? - java

我有一个带有经过验证的路线的游戏应用程序。我实现了一个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来简化我的应用程序,但是如何在测试中模拟该身份验证器?

谢谢 :)

java大神给出的解决方案

找到了解决方法。我只使用了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绑定到所需的实现。

java:继承 - java

有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…

Java-如何将此字符串转换为日期? - java

我从服务器收到此消息,我不明白T和Z的含义,2012-08-24T09:59:59Z将此字符串转换为Date对象的正确SimpleDateFormat模式是什么? java大神给出的解决方案 这是ISO 8601标准。您可以使用SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM…

从方法返回数组-Java - java

private static Coordinate[] getCircleCoordintaes() { Coordinate coordinates[] = {new Coordinate(0, 0)}; return coordinates; } 以上程序工作正常。在上面的程序中,返回的坐标数组首先初始化了数组使用这条线Coordinate coordi…

Java Swing SearchBox模型 - java

我需要使用Java Swing的搜索框,如果单击任何建议,当输入字母时它将显示来自数据库的建议,它将执行一些操作。如果有可能在Java swing中,请提供源代码提前致谢 java大神给出的解决方案 您可以使用DefaultComboBoxModel,输出将是这样。Try this在此代码中,您将找到countries数组,因此您需要从数据库中获取此数组。

JAVA:如何检查对象数组中的所有对象是否都是子类的对象? - java

我有一个对象数组。现在,我要检查所有这些对象是否都是MyObject的实例。有没有比这更好的选择:boolean check = true; for (Object o : justAList){ if (!(o instanceof MyObject)){ check = false; break; } } java大神给出的解决方案 如果您不喜欢循环,则…