ReflectionSetterInvoker { public boolean invokeSetter(Object setterHostObject, Object forArg) { Method setter = findSetter(setterHostObject.getClass(), forArg); if(setter == null) { return false; } invokerSetter(setter, setterHostObject, forArg); return true; }  boolean invokeSetter(Object setterHostObject, Object forArg); static void invokerSetter(Method setter, Object setterHostObject, Object argument); Method findSetter(Class<?> setterHostClass, Object forArg);  }
@Test public void shouldInvokePubliclyAvailableSetter() { ReflectionSetterInvokerTestObject testObject = new ReflectionSetterInvokerTestObject(); boolean invoked = reflectionSetterInvoker.invokeSetter(testObject, "TEST"); assertThat(invoked).isTrue(); assertThat(testObject.inconventionalSetterArg).isEqualTo("TEST"); }
@Test public void shouldInvokePrivateSetter() { ReflectionSetterInvokerTestObject testObject = new ReflectionSetterInvokerTestObject(); boolean invoked = reflectionSetterInvoker.invokeSetter(testObject, Integer.valueOf(1)); assertThat(invoked).isTrue(); assertThat(testObject.privateSetterArg).isEqualTo(1); }
@Test public void shouldReturnFalseWhenSetterNotFound() { ReflectionSetterInvokerTestObject testObject = new ReflectionSetterInvokerTestObject(); boolean invoked = reflectionSetterInvoker.invokeSetter(testObject, Long.valueOf(1)); assertThat(invoked).isFalse(); }