【问题标题】:Unit Testing Guice modules where one module installs another module一个模块安装另一个模块的单元测试 Guice 模块
【发布时间】:2020-03-18 08:49:03
【问题描述】:

仅供参考,我正在使用 Mockito 和 TestNg

我知道如何在 Guice 模块中使用 @Bind 模拟我的外部依赖项来测试我的逻辑。

这里我有一个模块(比如Foo),它在configure 方法中有install(new Bar());

可以在Foo中绑定各种外部依赖,但是不知道如何处理Bar中的东西。

例如)

public class FooTest {

@Bind
@Mock
SomeExternalDependency1 someExternalDependency1;

@Bind
@Mock
SomeExternalDependency2 someExternalDependency2;

@BeforeClass
public void setup() {
    MockitoAnnotations.initiMocks(this);
    injector = Guice.createInjector(Modules.override(new Foo())with(
        new TestFooModule()), BoundFieldModule.of(this));
    injector.injectMembers(this);
}

@Test
public void testSomething() {
    //asssert something here
}

static class TestFooModule extends AbstractModule {
    @Override
    protected void configure() { }
}

但是当我运行这个测试时,它抱怨Bar 中的外部依赖项。

如何在不实例化 Bar 模块的情况下测试 Foo 模块?

对于不安装“子”模块的模块,这种测试工作正常。

【问题讨论】:

    标签: java testng guice


    【解决方案1】:

    我需要bind@provides 来自TestFooModule 内部的 Bar。这解决了我的问题。

    【讨论】:

      猜你喜欢
      • 2011-10-30
      • 1970-01-01
      • 2016-08-28
      • 2022-01-23
      • 1970-01-01
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多