@Test public void autoInstantiationOfIncludedModules() { // Have to make these non-method-scoped or instantiation errors occur. ObjectGraph objectGraph = ObjectGraph.create(new TestModuleB()); // TestModuleA auto-created. assertThat(objectGraph.get(A.class)).isNotNull(); assertThat(objectGraph.get(B.class).a).isNotNull(); }
@Test public void childModuleWithChildModule() { @Module(injects = TestEntryPoint.class, includes = ModuleWithChildModule.class) class TestModule {} ObjectGraph objectGraph = ObjectGraph.create(new TestModule()); TestEntryPoint entryPoint = new TestEntryPoint(); objectGraph.inject(entryPoint); assertThat(entryPoint.s).isEqualTo("injected"); }
@Test public void childModuleWithManualConstruction() { @Module(injects = TestEntryPoint.class, includes = ModuleWithConstructor.class) class TestModule {} ObjectGraph objectGraph = ObjectGraph.create(new ModuleWithConstructor("a"), new TestModule()); TestEntryPoint entryPoint = new TestEntryPoint(); objectGraph.inject(entryPoint); assertThat(entryPoint.s).isEqualTo("a"); }
@Test public void childModuleWithEntryPoint() { @Module(includes = ModuleWithEntryPoint.class) class TestModule { @Provides String provideString() { return "injected"; } } ObjectGraph objectGraph = ObjectGraph.create(new TestModule()); TestEntryPoint entryPoint = objectGraph.get(TestEntryPoint.class); assertThat(entryPoint.s).isEqualTo("injected"); }
@Test public void childModuleWithStaticInjection() { @Module(includes = ModuleWithStaticInjection.class) class TestModule { @Provides String provideString() { return "injected"; } } ObjectGraph objectGraph = ObjectGraph.create(new TestModule()); TestStaticInjection.s = null; objectGraph.injectStatics(); assertThat(TestStaticInjection.s).isEqualTo("injected"); }
@Test public void moduleExtendingClassThrowsException() { try { ObjectGraph.create(new ThreadModule()); fail(); } catch (IllegalArgumentException e) { assertThat(e.getMessage()).startsWith("Modules must not extend from other classes: "); } }
@Test public void childModuleMissingManualConstruction() { @Module(includes = ModuleWithConstructor.class) class TestModule {} try { ObjectGraph.create(new TestModule()); fail(); } catch (IllegalArgumentException expected) { } }
@Test public void provideRawLazyFails() { @Module class ProvidesRawLazyModule { @Provides Lazy provideObject() { return null; } } try { ObjectGraph.create(new ProvidesRawLazyModule()); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage()) // .startsWith("@Provides method must not return Lazy directly: ") .endsWith("ProvidesRawLazyModule.provideObject"); } }
@Test(expected = IllegalArgumentException.class) public void childModuleMissingModuleAnnotation() { ObjectGraph.create(new ChildModuleMissingModuleAnnotation()); }
@Test public void autoInstantiationOfModules() { // Have to make these non-method-scoped or instantiation errors occur. ObjectGraph objectGraph = ObjectGraph.create(TestModuleA.class); assertThat(objectGraph.get(A.class)).isNotNull(); }
private <T> T injectWithModule(T ep, Object... modules) { // TODO(cgruber): Make og.inject(foo) return foo properly. ObjectGraph og = ObjectGraph.get(modules); og.inject(ep); return ep; }