DependencySupplierImpl extends BaseDependencySupplier { @Override public Object getInstance(Class<?> type) { return dependencies.get(type); } DependencySupplierImpl();  DependencySupplierImpl(Class<? extends Annotation> injectAnnotation); void addInstance(Class<?> key, Object instance); @Override Object getInstance(Class<?> type);  }
@Test public void providesNullInstanceForFieldsAnnotatedWithNotSupportedAnnotation() throws Exception { DependencySupplierImpl dependencySupplier = createDependencySupplier("foo", -1); java.lang.reflect.Field field = SomeType.class.getDeclaredField("annotatedWithCustomAnnotation"); FieldInjector fieldInjector = createFieldInjector(field); Object instance = dependencySupplier.getInstance(fieldInjector); assertNull(instance); }
@Test public void providesInstanceByParameter() throws Exception { DependencySupplierImpl dependencySupplier = createDependencySupplier("foo", -1); Object instance = dependencySupplier.getInstance(createParameter(String.class, null, null)); assertEquals("foo", instance); }
@Test public void providesInstanceProviderByParameter() throws Exception { DependencySupplierImpl dependencySupplier = createDependencySupplier("foo", -1); ParameterizedType stringProviderParameterizedType = newParameterizedType(Provider.class, String.class); Object instance = dependencySupplier.getInstance(createParameter(Provider.class, stringProviderParameterizedType, null)); assertTrue( String.format("Instance of javax.inject.Provider expected but %s found", instance == null ? "null" : instance.getClass()), instance instanceof Provider); assertEquals("foo", ((Provider)instance).get()); }
@Test public void providesInstanceByType() throws Exception { DependencySupplierImpl dependencySupplier = createDependencySupplier("foo", -1); Object instance = dependencySupplier.getInstance(String.class); assertEquals("foo", instance); }
@Test public void providesInstanceForInjectAnnotatedField() throws Exception { DependencySupplierImpl dependencySupplier = createDependencySupplier("foo", -1); java.lang.reflect.Field field = SomeType.class.getDeclaredField("annotated"); FieldInjector fieldInjector = createFieldInjector(field); Object instance = dependencySupplier.getInstance(fieldInjector); assertEquals("foo", instance); }
@Test public void looksUpInstanceForInjectAnnotatedNamedField() throws Exception { DependencySupplierImpl dependencySupplier = spy(createDependencySupplier("foo", -1)); java.lang.reflect.Field field = SomeType.class.getDeclaredField("annotatedAndNamed"); FieldInjector fieldInjector = createFieldInjector(field); dependencySupplier.getInstance(fieldInjector); verify(dependencySupplier).getInstance(fieldInjector); }
@Test public void providesInstanceForInjectFieldAnnotatedWithCustomAnnotation() throws Exception { DependencySupplierImpl dependencySupplier = createDependencySupplier(MyInject.class, "foo", -1); java.lang.reflect.Field field = SomeType.class.getDeclaredField("annotatedWithCustomAnnotation"); FieldInjector fieldInjector = createFieldInjector(field); Object instance = dependencySupplier.getInstance(fieldInjector); assertEquals("foo", instance); }
@Test public void providesNullInstanceForNotAnnotatedField() throws Exception { DependencySupplierImpl dependencySupplier = createDependencySupplier("foo", -1); java.lang.reflect.Field field = SomeType.class.getDeclaredField("notInjectable"); FieldInjector fieldInjector = createFieldInjector(field); Object instance = dependencySupplier.getInstance(fieldInjector); assertNull(instance); }