How to test implementations of Guice AbstractModule?
Asked Answered
M

3

15

How to test implementations of Guice AbstractModule in a big project without creating fake implementations? Is it possible to test bind() and inject() methods?

Marcellus answered 3/11, 2014 at 8:14 Comment(1)
You do not want to test the framework, so just trust guice that bind and inject are working fine. If you want to test your module implementations, have a look at Modules.overwrite, you can keep your production modules and overwrite just enough fake/mock stuff so you can still easily unit test them.Intyre
L
18

Typically the best way to test Guice modules is to just create an injector in your test and ensure you can get instances of keys you care about out of it.

To do this without causing production stuff to happen you may need to replace some modules with other modules. You can use Modules.override to selectively override individual bindings, but you're usually better off just not installing "production" type modules and using faked bindings instead.

Since Guice 4.0 there's a helper class BoundFieldModule that can help with this. I often set up tests like:

public final class MyModuleTest {
  @Bind @Mock DatabaseConnection dbConnection;
  @Bind @Mock SomeOtherDependency someOtherDependency;

  @Inject Provider<MyThing> myThingProvider;

  @Before public void setUp() {
    MockitoAnnotations.initMocks(this);
    Guice.createInjector(new MyModule(), BoundFieldModule.of(this))
        .injectMembers(this);
  }

  @Test public void testCanInjectMyThing() {
    myThingProvider.get();
  }
}

There's more documentation for BoundFieldModule on the Guice wiki.

Lacerated answered 7/11, 2014 at 19:47 Comment(3)
Thank you for answer. Sorry, but I can't resolve BoundFieldModule class and @Bind annotation.Marcellus
@Nikolas: You can do the same thing by just creating an anonymous AbstractModule subclass in your test, of course -- BoundFieldModule is just a shortcut. (If it's not available in your project, you might want to upgrade Guice to the latest version.) The important part of the answer is that the best way to ensure your dependencies are correct is to just create an Injector in your test, and verify it works as expected.Lacerated
@Marcellus I had to add the guice-testlib artifact to maven depdencies, it’s under the com.google.inject.extensions groupId. Version 4.2.0 is listed on Maven central but only 4.1.0 would download for me.Jamesjamesian
R
4

You can simply test a module implementation by creating the Injector and then assert the bindings by calling getInstance():

Injector injector = Guice.createInjector(new SomeModule());
assertNotNull(injector.getInstance(SomeSingleton.class));
Rasberry answered 6/9, 2020 at 15:1 Comment(0)
P
0

An improvement to above solution is to test that correct instance is bound:

    class MyModuleTests {
        @Test
        void testDependencies() {
            final var injector = Guice.createInjector(MyModule());
            assertInstanceOf(MyClassImpl.class, injector.getInstance(MyClass.class));

        }
}
Pleione answered 29/1 at 13:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.