FieldImporter { @SuppressWarnings("unchecked") void importAnnotatedFields(Object importFrom) { try { for (Field field : ReflectUtil.getAllDeclaredFields(importFrom.getClass())) { if (field.isAnnotationPresent(Unmapped.class) || !ReflectUtil.isAnyAnnotationPresent(field, mImportAnnotations)) { continue; } field.setAccessible(true); Object fieldValue = field.get(importFrom); if (fieldValue != null) { DependencyKey key = mKeyAdjuster.adjustKey(DependencyKey.fromField(field), field); mDependencyMap.put(key, fieldValue, null); } } } catch (IllegalAccessException e) { throw new RuntimeException(e); } } FieldImporter(
      Collection<Class<? extends Annotation>> importAnnotations,
      DependencyMap dependencyMap,
      @Nullable KeyAdjuster keyAdjuster);   }
@Test public void testImportingRealObjects() throws NoSuchFieldException { initImporter(RealObject.class); mImporter.importAnnotatedFields(this); verify(mDependencyMap).put(key1, testObj1, null); verify(mKeyAdjuster).adjustKey(key1, this.getClass().getDeclaredField("testObj1")); verify(mDependencyMap).put(key2, testObj2, null); verify(mKeyAdjuster).adjustKey(key2, this.getClass().getDeclaredField("testObj2")); verify(mDependencyMap).put(key3, testObj3, null); verify(mKeyAdjuster).adjustKey(key3, this.getClass().getDeclaredField("testObj3")); verifyNoMoreInteractions(mDependencyMap); }
@Test public void testImportingMocks() throws NoSuchFieldException { initImporter(Mock.class); mImporter.importAnnotatedFields(this); verify(mDependencyMap).put(key5, testObj5, null); verify(mKeyAdjuster).adjustKey(key5, this.getClass().getDeclaredField("testObj5")); verify(mDependencyMap).put(key6, testObj6, null); verify(mKeyAdjuster).adjustKey(key6, this.getClass().getDeclaredField("testObj6")); verify(mDependencyMap).put(key7, testObj7, null); verify(mKeyAdjuster).adjustKey(key7, this.getClass().getDeclaredField("testObj7")); verifyNoMoreInteractions(mDependencyMap); }
@Test public void testImportBothMocksAndRealObjects() { initImporter(Arrays.asList(Mock.class, RealObject.class)); mImporter.importAnnotatedFields(this); verify(mDependencyMap).put(key1, testObj1, null); verify(mDependencyMap).put(key2, testObj2, null); verify(mDependencyMap).put(key3, testObj3, null); verify(mDependencyMap).put(key5, testObj5, null); verify(mDependencyMap).put(key6, testObj6, null); verify(mDependencyMap).put(key7, testObj7, null); verifyNoMoreInteractions(mDependencyMap); }
@Test public void testImportFromSubAndSuperClasses() { SubclassTestObject testObject = new SubclassTestObject(); DependencyKey<SubclassTestObject.SubClassInnerClass> subClassInnerClassKey = DependencyKey.of(SubclassTestObject.SubClassInnerClass.class); DependencyKey<SuperclassTestObject.SuperClassInnerClass> superClassInnerClassKey = DependencyKey.of(SuperclassTestObject.SuperClassInnerClass.class); MockitoAnnotations.initMocks(testObject); initImporter(Arrays.asList(RealObject.class, Mock.class)); mImporter.importAnnotatedFields(testObject); verify(mDependencyMap).put(key1, "subclass", null); verify(mDependencyMap).put(key2, "superclass", null); verify(mDependencyMap).put( eq(subClassInnerClassKey), any(SubclassTestObject.SubClassInnerClass.class), nullable(DependencyValidator.class)); verify(mDependencyMap).put( eq(superClassInnerClassKey), any(SuperclassTestObject.SuperClassInnerClass.class), nullable(DependencyValidator.class)); verifyNoMoreInteractions(mDependencyMap); }