【发布时间】:2016-01-14 21:51:41
【问题描述】:
我的应用程序是用 Guice 构建的。应用程序模块使用 Multibinder 绑定接口的多个实现。
Multibinder<FooInterface> newSetBinder = Multibinder.newSetBinder(binder(), FooInterface.class);
newSetBinder.addBinding().to(FooInterfaceImpl.class);
newSetBinder.addBinding().to(BarInterfaceImpl.class);
此应用程序模块的测试类负责确保绑定适当的实现。对于非 Multibound 绑定,使用 Injector.getInstance(Class<T> type) 很简单:
assertThat("Incorrect implementation bound", injector.getInstance(SomeInterface.class), instanceOf(SomeInterfaceImpl.class));
但是,这不适用于 Multibound 接口。我想返回给定接口的绑定集合,并断言集合包含预期的类。
有没有办法做到这一点?
【问题讨论】: