【问题标题】:Test Error Maven RoboGuice [No implementation for "class" was bound]测试错误 Maven RoboGuice [没有绑定“类”的实现]
【发布时间】:2012-06-12 20:31:05
【问题描述】:

我有一个 Android 项目,其中包含两个源文件夹 src 和 test。在测试中,我有我的测试课程和一些模拟课程。我在编写测试的某个类中使用了 Android 的 RoboGuice 依赖注入。

测试在模拟器上的 Eclipse 中运行得非常好,但使用 maven clean install 失败。

No implementation for com.Store<com.MessageEvent> was bound.

使用注射器时测试在设置时失败。

mm = Guice.createInjector(new TestModule()).getInstance(MM.class);

这是我的绑定模块:

    public class TestModule implements Module{
    @Override
    public void configure(com.google.inject.Binder binder) {
        binder.bind(Context.class).toInstance(getContext());
        binder.bind(Scheduler.class).to(MockScheduler.class);
        binder.bind(EventManager.class).to(MockEventManager.class);
        binder.bind(new TypeLiteral<Store<Message>>(){}).to(new TypeLiteral<JsonStore<Message>>(){});
        binder.bind(new TypeLiteral<Store<MessageEvent>>(){}).to(new TypeLiteral<JsonStore<MessageEvent>>(){});
    }

    @Provides 
    JsonStore<MessageEvent> provideMessageEventJsonStore(Context context){
        return new JsonStore<MessageEvent>(context, "message_events_test.json", MessageEvent.class);
    }

    @Provides
    JsonStore<Message> provideMessageJsonStore(Context context){
        return new JsonStore<Message>(context, "message_manager_test.json", Message.class); 
    }
}

为什么在 Maven 中运行测试时会抛出异常,而不是在 Eclipse 中?

【问题讨论】:

  • 我以某种方式解决了我的问题,尽管我不确定它为什么会起作用。我提取了 EvenManager 的接口并将接口绑定到 mock。然后,我将我的模拟包(位于我的测试源文件夹下)移动到我的 src 源文件夹下。 maven 现在运行良好。

标签: android maven roboguice


【解决方案1】:

“未绑定任何实现”表示您需要在模块文件中为此特定类添加一行。

【讨论】:

  • 这不是我在做什么吗? binder.bind(new TypeLiteral&lt;Store&lt;MessageEvent&gt;&gt;(){}).to(new TypeLiteral&lt;JsonStore&lt;MessageEvent&gt;&gt;(){});
  • 您使用的是什么版本的 Roboguice?
  • &lt;dependency&gt; &lt;groupId&gt;org.roboguice&lt;/groupId&gt; &lt;artifactId&gt;roboguice&lt;/artifactId&gt; &lt;version&gt;2.0&lt;/version&gt; &lt;/dependency&gt;
  • 你能验证modules文件的格式是否正确吗?我的不一样。它有“bind(.....).to(.....)”,而不是“binder.bind(.....”并且配置方法不带参数。我假设你使用 Robolectric?如果没有,我建议您使用 Robolectric 进行单元测试。
  • 我有一个 binder 变量传递给 configure 方法,因为我实现了 Module 而不是扩展 AbstractModule。我不使用 Robolectric,我在模拟器中运行我的测试。我会调查一下,非常感谢您的宝贵时间!
猜你喜欢
  • 1970-01-01
  • 2014-03-19
  • 2011-12-15
  • 2013-08-14
  • 1970-01-01
  • 2018-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多