【问题标题】:JUnit test with GIN injection, without GWTTestCase and overloading gin modules?使用 GIN 注入、没有 GWTTestCase 和重载 gin 模块的 JUnit 测试?
【发布时间】:2013-01-26 06:14:46
【问题描述】:

我使用 Guice/Gin 设计了一个新项目,因此我可以使我们的代码更加模块化和可交换,尤其是在测试时。

但是,我不知道如何在实践中进行这项工作。我的印象是我可以在我的测试中创建一个新的 Gin/Guice 模块并安装我的“基本”模块,重载我想用特定测试实现替换的任何绑定。

我不想使用 GWTTestCase 并加载我的整个模块,因为对于我需要执行的精细测试类型而言,它非常缓慢且不方便。

我尝试过使用 Jukito (http://code.google.com/p/jukito/)、gwt-test-utils (http://code.google.com/p/gwt-test-utils/wiki/HowToUseWithGIN) 以及一些使用 guice (http://fabiostrozzi.eu/2011/03/27/junit-tests-easy-guice/) 的资源。

这些方法都没有产生任何结果。

如果我为我的 Gin 模块定义了一个镜像 guice 模块,我认为 Guice 方法可能会奏效。但是我真的不想同时管理这两个。我真的只是想测试我的 GIN 模块,就像我假设人们使用 Guice 进行测试一样。

我觉得这应该很简单,谁能给我指出有效的例子?

更新

看待这个问题的另一种方式是:

当我注入的类位于外部 Gin 模块中时,如何让 Jukito 网站 (http://code.google.com/p/jukito/) 上的示例正常工作?

**更新 - 参考 Thomas Boyer 的回答 **

感谢 Tom 的提示,我找不到使用适配器的示例,但我尝试扩充 Jukito 示例以使用 GinModuleAdapter:

@RunWith(JukitoRunner.class)
public class MyGinTest {

  public static class Module extends JukitoModule {
    protected void configureTest() {
        install(new GinModuleAdapter(new ClientModule()));
    }
  }

  @Test
  @Inject
  public void testAdd(SyncedDOMModel mod){
      assertNotNull(mod);
  }
}

当我尝试运行这个测试时,我收到了这个异常:

java.lang.AssertionError: should never be actually called
    at com.google.gwt.inject.rebind.adapter.GwtDotCreateProvider.get(GwtDotCreateProvider.java:43)
    at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:40)
    at com.google.inject.internal.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:46)
    at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1031)
    at com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
    at com.google.inject.Scopes$1$1.get(Scopes.java:65)
    at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:40)
    at com.google.inject.internal.InternalInjectorCreator$1.call(InternalInjectorCreator.java:204)
    at com.google.inject.internal.InternalInjectorCreator$1.call(InternalInjectorCreator.java:198)
    at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1024)
    at com.google.inject.internal.InternalInjectorCreator.loadEagerSingletons(InternalInjectorCreator.java:198)
    at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:179)
    at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:109)
    at com.google.inject.Guice.createInjector(Guice.java:95)
    at com.google.inject.Guice.createInjector(Guice.java:72)
    at com.google.inject.Guice.createInjector(Guice.java:62)
    at org.jukito.JukitoRunner.ensureInjector(JukitoRunner.java:118)
    at org.jukito.JukitoRunner.computeTestMethods(JukitoRunner.java:177)
    at org.jukito.JukitoRunner.validateInstanceMethods(JukitoRunner.java:276)
    at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:102)
    at org.junit.runners.ParentRunner.validate(ParentRunner.java:344)
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:74)
    at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:55)
    at org.jukito.JukitoRunner.<init>(JukitoRunner.java:72)

我的 gin 模块是 GWTP 项目的一部分,如下所示:

public class ClientModule extends AbstractPresenterModule {

    @Override
    protected void configure() {
        install(new DefaultModule(ClientPlaceManager.class));

        bindPresenter(MainPagePresenter.class, MainPagePresenter.MyView.class,
                MainPageView.class, MainPagePresenter.MyProxy.class);

        bindConstant().annotatedWith(DefaultPlace.class).to(NameTokens.main);

        bindPresenterWidget(MapTreePresenter.class,
                MapTreePresenter.MyView.class, MapTreeView.class);

        bindPresenterWidget(MapTreeItemPresenter.class,
                MapTreeItemPresenter.MyView.class, MapTreeItemView.class);

        bind(ResourcePool.class).to(DefferredResourcePool.class);

        bind(WebSocket.class).to(WebSocketImpl.class);

    }

}

如您所见,我在测试SyncedDOMModel 中注入的类使用了我在模块中绑定的WebSocket。当我测试时,我不想使用真正的 websocket 和服务器。所以我想在我的测试中重载这个绑定,用一个基本上模拟整个事情的类。在这种情况下,只注入 WebSocket 的不同实现比使用模拟更容易。

如果有帮助,这是 SyncedDOMMOdel 类的基本大纲:

public class SyncedDOMMOdel {
 ....
    @Inject
    public SyncedDOMModel(WebSocket socket){
        this.socket = socket;
    }

 ....
}

【问题讨论】:

    标签: gwt junit guice gwt-gin jukito


    【解决方案1】:

    您可以使用GinModuleAdapter 来使用任何GinModule 作为Guice Module

    显然,您不会从 GIN 的特定功能中受益:当某些东西没有特定绑定时默认为 GWT.create()(这包括接口和抽象类,这会在 Guice 中抛出),并自动搜索 RemoteService 接口当名称以Async 结尾的接口没有特定绑定时。
    而且您将无法使用任何依赖于 JSNI 或延迟绑定 (GWT.create()) 的东西,就像在任何非 GWTTestCase 单元测试中一样。

    【讨论】:

    • 您好 Thomas,感谢您的指导!我根据您的建议更新了我上面的帖子,提供了更多详细信息。我觉得我很接近,但仍然没有把整个事情放在一起。任何更多的帮助将不胜感激!
    • 查看更新的答案。如果我是你,我会在GwtDotCreateProvider(或调用堆栈中的另一个方法)中设置一个断点,以了解哪个类缺少显式绑定。注意:它可能只是一个没有用@Inject 注释的零参数构造函数的类(这对Guice 没用,但IIRC 缺少@Inject 会触发GWT.create(X.class) 而不是new X() 与GIN)
    • 谢谢托马斯!在玩了一些之后,我能够得到这个工作。使用 GinModuleAdapter,我什至可以让覆盖工作!太棒了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-16
    • 1970-01-01
    • 2020-08-23
    • 2011-10-28
    • 2016-08-17
    • 1970-01-01
    相关资源
    最近更新 更多