DiResolverUtil { static void setValue(PmObject pm, Field field, boolean nullAllowed, Object value) { validateValidValue(pm, field, nullAllowed, value); try { ensureAccessibility(field); field.set(pm, value); } catch (Exception ex) { throw new PmRuntimeException(pm, "Can't initialize field '" + field.getName() + "' in class '" + field.getDeclaringClass().getName() + "'.", ex); } }  static DiResolver[] getDiResolvers(Class<?> forClass);  }
@Test public void setValueWithValidValueActuallySetsTheValue() { DiResolverUtil.setValue(myPm, myFieldInitializedNullPropField, true, null); assertNull(myPm.myFieldInitializedNullProp, null); DiResolverUtil.setValue(myPm, myFieldInitializedPropField, false, "[some non-null value]"); assertEquals("[some non-null value]", myPm.myFieldInitializedProp); DiResolverUtil.setValue(myPm, mySetterInitializedNullPropSetter, true, null); assertNull(myPm.mySetterInitializedNullProp); DiResolverUtil.setValue(myPm, mySetterInitializedPropSetter, false, "[some non-null value]"); assertEquals("[some non-null value]", myPm.mySetterInitializedProp); }
@Test public void setValueWithInvalidValueThrowsExceptionForNullValueIfNullNotAllowed() { expectedException.expect(PmRuntimeException.class); expectedException.expectMessage( "Found value for dependency injection of '" + myFieldInitializedNullPropField + "' was null." + "\nPlease check your managed objects or configure lazy null-value handling using @PmInject(nullAllowed=true)."); DiResolverUtil.setValue(myPm, myFieldInitializedNullPropField, false, null); }
@Test public void setValueDirectThrowsExceptionIfFieldCantBeSet() { expectedException.expect(PmRuntimeException.class); expectedException.expectMessage("Can't initialize field 'myAlienFakeProp'"); DiResolverUtil.setValue(myPm, myAlienFakePropField, false, "[some value]"); }
@Test public void setValueViaSetterThrowsExceptionIfFieldCantBeSet() { expectedException.expect(PmRuntimeException.class); expectedException.expectMessage("Can't invoke method 'setMyAlienProp' in class 'org.pm4j.core.pm.impl.inject.DiResolverUtilTest$MyPm'."); DiResolverUtil.setValue(myPm, myAlienPropSetter, false, "[some value]"); }