@Test public void validateSetBinding() { class TestEntryPoint { @Inject Set<String> strings; } @Module(injects = TestEntryPoint.class) class TestModule { @Provides(type=SET) String provideString1() { return "string1"; } @Provides(type=SET) String provideString2() { return "string2"; } } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); graph.validate(); }
@Test public void entryPointNeedsNoInjectAnnotation() { @Module(injects = NoInjections.class) class TestModule { } ObjectGraph.createWith(new TestingLoader(), new TestModule()).validate(); }
@Test public void unusedProvidesMethod_whenModuleLibrary_passes() throws Exception { class EntryPoint { } class BagOfMoney { } @Module(injects = EntryPoint.class, library = true) class TestModule { @Provides BagOfMoney providesMoney() { return new BagOfMoney(); } } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); graph.validate(); }
@Test public void validateEmptySetBinding() { class TestEntryPoint { @Inject Set<String> strings; } @Module(injects = TestEntryPoint.class) class TestModule { @Provides(type=SET_VALUES) Set<String> provideDefault() { return emptySet(); } } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); graph.validate(); }
@Test public void validateLazy() { @Module(library = true) class TestModule { @Provides Integer dependOnLazy(Lazy<String> lazyString) { throw new AssertionError(); } @Provides String provideLazyValue() { throw new AssertionError(); } } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); graph.validate(); }
@Test public void validateLibraryModules() { class TestEntryPoint {} @Module(library = true) class SetModule { @Provides(type = SET) public String provideString() { return ""; } } @Module(injects = TestEntryPoint.class, includes = SetModule.class) class TestModule {} ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule(), new SetModule()); graph.validate(); }
/** 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 objectGraphGetInterface() { final Runnable runnable = new Runnable() { @Override public void run() { } }; @Module(injects = Runnable.class) class TestModule { @Provides Runnable provideRunnable() { return runnable; } } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); graph.validate(); assertThat(graph.get(Runnable.class)).isSameAs(runnable); }
@Test public void noProvideBindingsForAbstractClasses() { class TestEntryPoint { @Inject AbstractList abstractList; } @Module(injects = TestEntryPoint.class) class TestModule { } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.validate(); fail(); } catch (IllegalStateException expected) { } }
@Test public void twoAtInjectConstructorsIsRejected() { @Module(injects = TwoAtInjectConstructors.class) class TestModule { @Provides String provideString() { throw new AssertionError(); } } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.validate(); fail(); } catch (IllegalStateException expected) { } }
@Test public void unusedSetBinding() throws Exception { @Module class TestModule { @Provides(type = Provides.Type.SET) String provideA() { throw new AssertionError(); } } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.validate(); fail(); } catch (IllegalStateException expected) { } }
@Test public void noJitBindingsForAnnotations() { class TestEntryPoint { @Inject @Named("a") A a; } @Module(injects = TestEntryPoint.class) class TestModule { } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.validate(); fail(); } catch (IllegalStateException expected) { } }
@Test public void noJitBindingsForInterfaces() { class TestEntryPoint { @Inject RandomAccess randomAccess; } @Module(injects = TestEntryPoint.class) class TestModule { } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.validate(); fail(); } catch (IllegalStateException expected) { } }
@Test public void nonEntryPointNeedsInjectAnnotation() { @Module class TestModule { @Provides String provideString(NoInjections noInjections) { throw new AssertionError(); } } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.validate(); fail(); } catch (IllegalStateException expected) { } }
@Test public void atInjectCircularDependenciesDetected() { class TestEntryPoint { @Inject Rock rock; } @Module(injects = TestEntryPoint.class) class TestModule { } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.validate(); fail(); } catch (RuntimeException expected) { } }
@Test public void unusedSetValuesBinding() throws Exception { @Module class TestModule { @Provides(type = Provides.Type.SET_VALUES) Set<String> provideA() { throw new AssertionError(); } } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.validate(); fail(); } catch (IllegalStateException expected) { } } }
@Test public void whenLibraryModulePlussedToNecessaryModule_shouldNotFailOnUnusedLibraryModule() throws Exception { class EntryPoint { } class BagOfMoney { } @Module(injects = EntryPoint.class, library = true) class ExampleLibraryModule { @Provides BagOfMoney providesMoney() { return new BagOfMoney(); } } @Module(injects = EntryPoint.class) class TestModule { } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); graph = graph.plus(new ExampleLibraryModule()); graph.validate(); }
@Test public void noConstructorInjectionsForClassesWithTypeParameters() { class TestEntryPoint { @Inject Parameterized<Long> parameterized; } @Module(injects = TestEntryPoint.class) class TestModule { @Provides String provideString() { return "injected"; } } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.validate(); fail(); } catch (IllegalStateException expected) { } }
@Test public void providesCircularDependenciesDetected() { @Module class TestModule { @Provides Integer provideInteger(String s) { throw new AssertionError(); } @Provides String provideString(Integer i) { throw new AssertionError(); } } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.validate(); fail(); } catch (RuntimeException expected) { } }
@Test public void unusedProviderMethod_whenNotLibraryModule_fails() throws Exception { class EntryPoint { } class BagOfMoney { } @Module(injects = EntryPoint.class) class TestModule { @Provides BagOfMoney providesMoney() { return new BagOfMoney(); } } try { ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); graph.validate(); fail("Validation should have exploded!"); } catch (IllegalStateException expected) { } }