DiResolverUtil { static void validateFieldIsNull(PmObject pm, Field f) { Object value = null; try { ensureAccessibility(f); value = f.get(pm); } catch (Exception ex) { throw new PmRuntimeException(pm, "Can't read field '" + f.getName() + "' in class '" + pm.getClass().getName() + "'.", ex); } if (value != null) { throw new PmRuntimeException(pm, "Can't initialize field '" + f.getName() + "' in class '" + pm.getClass().getName() + "'. Already has value: " + value ); } }  static DiResolver[] getDiResolvers(Class<?> forClass);  }
@Test public void validateFieldIsNullThrowsExceptionWhenFieldAlreadyContainsValue() { expectedException.expect(PmRuntimeException.class); expectedException.expectMessage("Can't initialize field 'myFieldInitializedNullProp' in class 'org.pm4j.core.pm.impl.inject.DiResolverUtilTest$MyPm'. Already has value: [faking some preexisting initialization]"); myPm.myFieldInitializedNullProp = "[faking some preexisting initialization]"; DiResolverUtil.validateFieldIsNull(myPm, ClassUtil.findField(MyPm.class, "myFieldInitializedNullProp")); }
@Test public void validateFieldIsNullThrowsExceptionWhenFieldIsNotReadable() { expectedException.expect(PmRuntimeException.class); expectedException.expectMessage("Can't read field 'myAlienFakeProp' in class 'org.pm4j.core.pm.impl.inject.DiResolverUtilTest$MyPm'."); DiResolverUtil.validateFieldIsNull(myPm, ClassUtil.findField(AnotherPm.class, "myAlienFakeProp")); }
@Test public void validateFieldIsNullDoesAcceptValidVales() { myPm.myFieldInitializedNullProp = null; DiResolverUtil.validateFieldIsNull(myPm, myFieldInitializedNullPropField); initMyPm(); assertEquals("[initialized directly]", myPm.myFieldInitializedProp); }