我刚开始玩Guice,我能想到的一个用例是在测试中我只想覆盖单个绑定.我想我想使用其余的生产级别绑定来确保正确设置所有内容并避免重复.
所以想象我有以下模块
public class ProductionModule implements Module {
public void configure(Binder binder) {
binder.bind(InterfaceA.class).to(ConcreteA.class);
binder.bind(InterfaceB.class).to(ConcreteB.class);
binder.bind(InterfaceC.class).to(ConcreteC.class);
}
}
Run Code Online (Sandbox Code Playgroud)
在我的测试中,我只想覆盖InterfaceC,同时保持InterfaceA和InterfaceB,所以我想要像:
Module testModule = new Module() {
public void configure(Binder binder) {
binder.bind(InterfaceC.class).to(MockC.class);
}
};
Guice.createInjector(new ProductionModule(), testModule);
Run Code Online (Sandbox Code Playgroud)
我也试过以下,没有运气:
Module testModule = new ProductionModule() {
public void configure(Binder binder) {
super.configure(binder);
binder.bind(InterfaceC.class).to(MockC.class);
}
};
Guice.createInjector(testModule);
Run Code Online (Sandbox Code Playgroud)
有谁知道是否有可能做我想做的事情,或者我完全咆哮错误的树?
---跟进:如果我在接口上使用@ImplementedBy标记,然后在测试用例中提供一个绑定,看起来我可以实现我想要的,当它之间存在1-1映射时,它可以很好地工作界面和实现.
此外,在与同事讨论之后,似乎我们将推翻覆盖整个模块并确保正确定义模块.这似乎可能会导致问题,虽然绑定在模块中放错位置并需要移动,因此可能会破坏大量测试,因为绑定可能不再可用于覆盖.