Search code examples
javatestngguice

Unit Testing Guice modules where one module installs another module


FYI, I am using Mockito and TestNg

I know how to test my logic in a Guice module by using @Bind to mock my external dependencies.

Here I have a module (say Foo) which has install(new Bar()); in the configure method.

I can bind the various external dependencies in Foo, but I don't know how to deal with things in Bar.

ex)

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() { }
}

But when I run this test, it complains about the external dependencies in Bar.

How do I test the Foo module without instantiating the Bar module?

For modules that don't install 'children' modules, this sort of testing works fine.


Solution

  • I needed to bind the @provides from Bar inside of the TestFooModule. That solved my issue.