### Question:
ReflectionHelper { public static void setFieldValue(final Object bean, final String fieldName, final Object value) { if (bean == null) { throw new HibersapException("Cannot set a value on a null object"); } try { Field declaredField = getDeclaredFieldWithInheritance(bean.getClass(), fieldName); declaredField.setAccessible(true); declaredField.set(bean, value); } catch (SecurityException | IllegalArgumentException | IllegalAccessException e) { throw new HibersapException("Can not assign an object of type " + getClassNameNullSafe(value) + " to the field " + bean.getClass().getName() + "." + fieldName, e); } } private  ReflectionHelper(); static Class<?> getClassForName(final String className); static Class<?> getArrayType(final Class<?> type); static Field getDeclaredField(final Object bean, final String fieldName); static Object getFieldValue(final Object bean, final String fieldName); static Class<?> getGenericType(final Field field); static T newCollectionInstance(final Class<T> clazz); static T newInstance(final Class<T> clazz); static T newInstance(final String fullyQualifiedClassName, final Class<T> superType); static List<T> createInstances(final List<String> fullyQualifiedClassNames, final Class<T> superType); static void setFieldValue(final Object bean, final String fieldName, final Object value); static Set<Field> getDeclaredFieldsWithAnnotationRecursively(final Class<?> clazz,
                                                                        final Class<? extends Annotation> annotationClass); static T[] newArrayFromCollection(final Collection<?> collection, final Class<T> elementType);  }### Answer:
@Test @SuppressWarnings("synthetic-access") public void setFieldValueCanChangeIntValue() { TestBean bean = new TestBean(); ReflectionHelper.setFieldValue(bean, "intValue", 2); assertThat(bean.intValue).isEqualTo(2); }
@Test(expected = HibersapException.class) public void setFieldValueThrowsExceptionWhenTryingToSetValueOnNullObject() { ReflectionHelper.setFieldValue(null, "intValue", 0); }
@Test(expected = HibersapException.class) public void setFieldValueThrowsExceptionWhenSettingNullValueOnPrimitiveType() { TestBean bean = new TestBean(); ReflectionHelper.setFieldValue(bean, "intValue", null); }
@Test public void setFieldValueCanSetNullValue() { TestBean bean = new TestBean(); bean.set = Collections.emptySet(); ReflectionHelper.setFieldValue(bean, "set", null); assertThat(bean.set).isNull(); }
@Test public void setFieldValueWhenFieldIsInheritedFromSuperclass() { TestSubClass bean = new TestSubClass(); ReflectionHelper.setFieldValue(bean, "set", Collections.emptySet()); assertThat(bean.getSet()).isNotNull(); }
@Test public void setFieldValueOnDirectMember() { TestSubClass bean = new TestSubClass(); ReflectionHelper.setFieldValue(bean, "paramSubClass", 3L); assertThat(bean.paramSubClass).isEqualTo(3L); }
@Test public void setFieldValueThrowsHibersapExceptionWithMessageContainingClassAndFieldNameWhenFieldDoesNotExist() { assertThatThrownBy(() -> ReflectionHelper.setFieldValue(new Object(), "doesNotExist", "someValue")) .isInstanceOf(HibersapException.class) .hasMessageContaining("Object") .hasMessageContaining("doesNotExist"); }