Refine search
@Test public void simpleInjectionWithUnGeneratedCode() { Entry$Point entryPoint = new Entry$Point(); ObjectGraph.create(new TestModule()).inject(entryPoint); assertThat(entryPoint.a).isEqualTo("a"); } }
@Test public void basicInjection() { ObjectGraph root = ObjectGraph.createWith(new TestingLoader(), new RootModule()); assertThat(root.get(A.class)).isNotNull(); assertThat(root.get(A.class)).isSameAs(root.get(A.class)); // Present and Singleton. assertThat(root.get(B.class)).isNotSameAs(root.get(B.class)); // Not singleton. assertFailInjectNotRegistered(root, C.class); // Not declared in RootModule. assertFailInjectNotRegistered(root, D.class); // Not declared in RootModule. // Extension graph behaves as the root graph would for root-ish things. ObjectGraph extension = root.plus(new ExtensionModule()); assertThat(root.get(A.class)).isSameAs(extension.get(A.class)); assertThat(root.get(B.class)).isNotSameAs(extension.get(B.class)); assertThat(root.get(B.class).a).isSameAs(extension.get(B.class).a); assertThat(extension.get(C.class).a).isNotNull(); assertThat(extension.get(D.class).c).isNotNull(); }
@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"); }
private void injectApplication() { if (shouldInjectApplication()) { graph.inject(this); graph.injectStatics(); } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Create the activity graph by .plus-ing our modules onto the application graph. DemoApplication application = (DemoApplication) getApplication(); activityGraph = application.getApplicationGraph().plus(getModules().toArray()); // Inject ourselves so subclasses will have dependencies fulfilled when this method returns. activityGraph.inject(this); }
@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 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(); }
/** https://github.com/square/dagger/issues/231 */ @Test public void atInjectAlwaysRequiredForConstruction() { @Module(injects = ArrayList.class) class TestModule { } ObjectGraph objectGraph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); objectGraph.validate(); try { objectGraph.get(ArrayList.class); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage()).contains("Unable to create binding for java.util.ArrayList"); } } }
@Test public void getInstance() { final AtomicInteger next = new AtomicInteger(0); @Module(injects = Integer.class) class TestModule { @Provides Integer provideInteger() { return next.getAndIncrement(); } } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); assertThat((int) graph.get(Integer.class)).isEqualTo(0); assertThat((int) graph.get(Integer.class)).isEqualTo(1); }
@Test public void privateFieldsFail() { class Test { @Inject private Object nope; } @Module(injects = Test.class) class TestModule { @Provides Object provideObject() { return null; } } try { ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(new Test()); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage()).contains("Can't inject private field: "); } }
private void assertFailInjectNotRegistered(ObjectGraph graph, Class<?> clazz) { try { assertThat(graph.get(clazz)).isNull(); } catch (IllegalArgumentException e) { assertThat(e.getMessage()).contains("No inject"); } } }
@Test public void staticFieldsNotInjectedByInjectMembers() { @Module( staticInjections = InjectsStaticAndNonStatic.class, injects = InjectsStaticAndNonStatic.class) class TestModule { @Provides String provideString() { throw new AssertionError(); } @Provides Integer provideInteger() { return 5; } } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); assertThat(InjectsStaticAndNonStatic.staticField).isNull(); InjectsStaticAndNonStatic object = new InjectsStaticAndNonStatic(); graph.inject(object); assertThat(InjectsStaticAndNonStatic.staticField).isNull(); assertThat(object.nonStaticField).isEqualTo(5); } }
@Override public void onCreate() { super.onCreate(); graph = ObjectGraph.create(getModules().toArray()); }
objectGraph = objectGraph.plus(rootModule); objectGraph.injectStatics(); objectGraph.inject(target); return objectGraph.get(type);
/** * Given a List<Object> with Dagger modules inside performs a plus over the application graph and * returns a new one with all the dependencies already created plus the one resolved with the * list of modules passed as argument. */ public ObjectGraph plusGraph(List<Object> activityScopeModules) { if (activityScopeModules == null) { throw new IllegalArgumentException( "You can't extend the application graph with a null list of modules"); } return graph.plus(activityScopeModules.toArray()); }
private <T> T injectWithModule(T ep, Object ... modules) { return ObjectGraph.createWith(new TestingLoader(), modules).inject(ep); } }
/** Inject the supplied {@code object} using the activity-specific graph. */ public void inject(Object object) { activityGraph.inject(object); } }