这是我们如何使用Guice在新应用程序中:
public class ObjectFactory {
private static final ObjectFactory instance = new ObjectFactory();
private final Injector injector;
private ObjectFactory() throws RuntimeException {
this.injector = Guice.createInjector(new Module1());
}
public static final ObjectFactory getInstance() {
return instance;
}
public TaskExecutor getTaskExecutor() {
return injector.getInstance(TaskExecutor.class);
}
}
Module1
定义 TaskExecutor
如何需要构建。
在代码中我们使用ObjectFactory.getInstance().getTaskExecutor()
获取TaskExecutor
的实例.
在单元测试中,我们希望能够将其替换为 FakeTaskExecutor
本质上我们想要获得 FakeTaskExecutor
的实例当ObjectFactory.getInstance().getTaskExecutor()
被调用。
我正在考虑实现 FakeModule
它将被注入(inject)器使用,而不是 Module1
.
在 Spring 中,我们只需使用 @Autowired
注释,然后为Test
定义单独的bean和Production
编写代码并使用 Spring4JunitRunner
运行我们的测试;我们正在尝试对 Guice 做类似的事情。
最佳答案
好吧,首先要做的事情是:您似乎没有按照预期的方式使用 Guice。一般来说,你想使用Guice.createInjector()
启动整个应用程序,并让它为您创建所有构造函数参数,而无需调用 new
.
典型的用例可能是这样的:
public class Foo {
private final TaskExecutor executor;
@Inject
public Foo(TaskExecutor executor) {
this.executor = executor;
}
}
这是可行的,因为 Foo 的实例是它们自己注入(inject)的,一直到对象图的上层。请参阅:Getting started
With dependency injection, objects accept dependencies in their constructors. To construct an object, you first build its dependencies. But to build each dependency, you need its dependencies, and so on. So when you build an object, you really need to build an object graph.
Building object graphs by hand is labour intensive, error prone, and makes testing difficult. Instead, Guice can build the object graph for you. But first, Guice needs to be configured to build the graph exactly as you want it.
因此,通常情况下,您不会创建单例模式并将注入(inject)器放入其中,因为您很少应该调用 Guice.createInstance
在你的主类(class)之外;让注入(inject)器为您完成所有工作。
综上所述,为了解决您实际询问的问题,您需要使用 Jukito .
The combined power of JUnit, Guice and Mockito. Plus it sounds like a cool martial art.
让我们回到上面描述的用例。在 Jukito 中,您可以写 FooTest
像这样:
@RunWith(JukitoRunner.class)
public class FooTest {
public static class Module extends JukitoModule {
@Override
protected void configureTest() {
bindMock(TaskExecutor.class).in(TestSingleton.class);
}
}
@Test
public void testSomething(Foo foo, TaskExecutor executor) {
foo.doSomething();
verify(executor, times(2)).someMethod(eq("Hello World"));
}
}
这将验证您的 Mock object ,由 Mockito 生成通过Jukito有方法someMethod
使用字符串 "Hello World"
调用它两次两次。
这就是为什么您不想使用 ObjectFactory
生成对象的原因以你描述的方式; Jukito 在其单元测试中为您创建了 Injector,而注入(inject) Mock 会非常困难,而且您必须编写大量样板文件。
关于java - 如何用假测试模块替换 Guice 模块进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31814851/