### Question:
ReflectionUtil { public static void injectSessionIntoTarget(final Object target, final Field field, final Session session) { if (!Session.class.isAssignableFrom(field.getType())) { throw new InternalHiberSapException( format("Annotation @%s can only be used on a field of type or subtype of %s. " + "Can not set %s field %s.%s to %s", HibersapSession.class.getSimpleName(), Session.class, field.getType(), target.getClass().getName(), field.getName(), session ) ); } boolean accessible = field.isAccessible(); synchronized (field) { field.setAccessible(true); try { field.set(target, session); } catch (IllegalAccessException e) { throw new InternalHiberSapException( format("Error injecting Hibersap Session %s into %s.%s", session, target.getClass().getName(), field.getName()), e ); } finally { field.setAccessible(accessible); } } } private  ReflectionUtil(); static Set<Field> getHibersapSessionFields(final Object target); static String getSessionManagerJndiName(final Field sessionField); static void injectSessionIntoTarget(final Object target, final Field field, final Session session);  }### Answer:
@Test public void injectsSessionIntoTarget() throws Exception { TestBean bean = new TestBean(); Session session = mock(Session.class); ReflectionUtil.injectSessionIntoTarget(bean, TestBean.class.getDeclaredField("session1"), session); assertThat(bean.session1).isSameAs(session); }
@Test(expected = InternalHiberSapException.class) public void injectSessionIntoTargetThrowsExceptionWhenFieldIsNotOfTypeHibersapSession() throws Exception { TestBean bean = new TestBean(); Session session = mock(Session.class); ReflectionUtil.injectSessionIntoTarget(bean, TestBean.class.getDeclaredField("notASession"), session); }