java - 如何用假测试模块替换 Guice 模块进行单元测试?

标签 java spring unit-testing guice

这是我们如何使用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/

相关文章:

java - 在 Tapestry 5.3 中链接多个选择组件(Ajax 更新)

java - 当 url 模式是路径时,无法让 Spring MVC 调度程序正常工作

java - 为什么 Spring 在处理端点异常时将 RuntimeException 包装在 NestedServletException 中?

java - FreeMarker如何在模板中调用object.toString()方法

java - spring.resources.static-locations 不适用于 devtools

javascript - 解析期间发生 fatal error 和 org.xml.sax.SAXParseException

python - 如何在 Python 的单元测试设置阶段实例化对象

c# - 为什么在单元测试中找不到 resx 文化?

java - 伪造 Ajax 请求表单以进行测试

java - Struts 1 ActionForm 中的 ArrayList 未填充