@Test public void moduleWithNoProvidesMethods() { @Module class TestModule { } ObjectGraph.createWith(new TestingLoader(), new TestModule()); }
@Test public void getLazyDoesNotCauseProvidesParametersToBeLoaded() { @Module class TestModule { @Provides Object provideObject(LazyProvidesParameter parameter) { throw new AssertionError(); } } ObjectGraph.createWith(new TestingLoader(), new TestModule()); assertThat(lazyProvidesParameterLoaded).isFalse(); }
@Test public void getLazyDoesNotCauseStaticsToBeLoaded() { @Module(staticInjections = LazyInjectStatics.class) class TestModule { } ObjectGraph.createWith(new TestingLoader(), new TestModule()); assertThat(LazyInjectStaticsLoaded).isFalse(); }
@Test public void getLazyDoesNotCauseInjectedTypesToBeLoaded() { @Module(injects = LazyEntryPoint.class) class TestModule { } ObjectGraph.createWith(new TestingLoader(), new TestModule()); assertThat(lazyEntryPointLoaded).isFalse(); }
@Test public void getLazyDoesNotCauseProvidesResultToBeLoaded() { @Module class TestModule { @Provides LazyProvidesResult provideLazy() { throw new AssertionError(); } } ObjectGraph.createWith(new TestingLoader(), new TestModule()); assertThat(lazyProvidesResultLoaded).isFalse(); }
@Test public void runtimeConstructorExceptionsAreNotWrapped() { @Module(injects = ThrowsOnConstruction.class) class TestModule { } try { ObjectGraph.createWith(new TestingLoader(), new TestModule()).get(ThrowsOnConstruction.class); fail(); } catch (ClassCastException e) { assertThat(e.getMessage()).isEqualTo("foo"); } }
@Test public void injectStatics() { @Module(staticInjections = InjectsOneField.class) class TestModule { @Provides String provideString() { return "static"; } } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(),new TestModule()); assertThat(InjectsOneField.staticField).isNull(); graph.injectStatics(); assertThat(InjectsOneField.staticField).isEqualTo("static"); }
@Test public void uninjectableSupertypes() { class TestEntryPoint { @Inject T t; } @Module(injects = TestEntryPoint.class) class TestModule { } TestEntryPoint entryPoint = new TestEntryPoint(); ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(entryPoint); assertThat(entryPoint.t).isNotNull(); }
@Test public void entryPointNeedsNoInjectAnnotation() { @Module(injects = NoInjections.class) class TestModule { } ObjectGraph.createWith(new TestingLoader(), new TestModule()).validate(); }
@Test public void childModuleWithEntryPoint() { @Module(includes = ModuleWithEntryPoint.class) class TestModule { @Provides String provideString() { return "injected"; } } ObjectGraph objectGraph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); TestEntryPoint entryPoint = objectGraph.get(TestEntryPoint.class); assertThat(entryPoint.s).isEqualTo("injected"); }
@Test public void childModuleMissingManualConstruction() { @Module(includes = ModuleWithConstructor.class) class TestModule { } try { ObjectGraph.createWith(new TestingLoader(), new TestModule()); fail(); } catch (IllegalArgumentException expected) { } }
@Test public void autoInstantiationOfModules() { // Have to make these non-method-scoped or instantiation errors occur. ObjectGraph objectGraph = ObjectGraph.createWith(new TestingLoader(), TestModuleA.class); assertThat(objectGraph.get(A.class)).isNotNull(); }
@Test public void childModuleWithBinding() { @Module( injects = TestEntryPoint.class, includes = ModuleWithBinding.class ) class TestModule { } ObjectGraph objectGraph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); TestEntryPoint entryPoint = new TestEntryPoint(); objectGraph.inject(entryPoint); assertThat(entryPoint.s).isEqualTo("injected"); }
@Test public void childModuleWithChildModule() { @Module( injects = TestEntryPoint.class, includes = ModuleWithChildModule.class ) class TestModule { } ObjectGraph objectGraph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); TestEntryPoint entryPoint = new TestEntryPoint(); objectGraph.inject(entryPoint); assertThat(entryPoint.s).isEqualTo("injected"); }
@Test public void getInstanceOfPrimitive() { @Module(injects = int.class) class TestModule { @Provides int provideInt() { return 1; } } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); assertEquals(1, (int) graph.get(int.class)); }
@Test public void autoInstantiationOfIncludedModules() { // Have to make these non-method-scoped or instantiation errors occur. ObjectGraph objectGraph = ObjectGraph.createWith(new TestingLoader(), new TestModuleB()); // TestModuleA auto-created. assertThat(objectGraph.get(A.class)).isNotNull(); assertThat(objectGraph.get(B.class).a).isNotNull(); }
@Test public void basicInjectionWithExtensionAndOverrides() { try { ObjectGraph.createWith(new TestingLoader(), new RootModule()).plus(new TestModule()); fail("Should throw exception."); } catch (IllegalArgumentException e) { assertEquals("TestModule: Module overrides cannot contribute set bindings.", e.getMessage()); } }
@Test public void basicInjectionWithExtension() { A a = new A(); ObjectGraph root = ObjectGraph.createWith(new TestingLoader(), new RootModule(a)); assertThat(root.get(A.class)).isSameAs(a); // Extension graph behaves as the root graph would for root-ish things. ObjectGraph extension = root.plus(new ExtensionModule()); assertThat(extension.get(A.class)).isSameAs(a); assertThat(extension.get(B.class).a).isSameAs(a); }
@Test public void testSingletonLinkingThroughExtensionGraph() { ObjectGraph root = ObjectGraph.createWith(new TestingLoader(), new RootModule()); // DO NOT CALL root.get(C.class)) HERE to get forced-linking behaviour from plus(); ObjectGraph extension = root.plus(new ExtensionModule()); assertThat(extension.get(SingletonLinkedFromExtension.class).c).isSameAs(root.get(C.class)); }
@Test public void basicInjectionWithExtension() { ObjectGraph root = ObjectGraph.createWith(new TestingLoader(), new RootModule()); RealSingleton rs = root.get(RealSingleton.class); assertThat(rs.ints).containsExactly(0, 1); ObjectGraph extension = root.plus(new ExtensionModule()); Main main = extension.get(Main.class); assertThat(main.ints).containsExactly(0, 1, 2, 3); // Second time around. ObjectGraph extension2 = root.plus(new ExtensionModule()); Main main2 = extension2.get(Main.class); assertThat(main2.ints).containsExactly(0, 1, 4, 5); }