### Question:
RequiredDataAdvice implements MethodBeforeAdvice { @SuppressWarnings("unchecked") protected static Collection<OpenmrsObject> getChildCollection(OpenmrsObject openmrsObject, Field field) { String fieldName = field.getName(); String getterName = "get" + StringUtils.capitalize(fieldName); try { if (field.isAnnotationPresent(AllowDirectAccess.class)) { boolean previousFieldAccessibility = field.isAccessible(); field.setAccessible(true); Collection<OpenmrsObject> childCollection = (Collection<OpenmrsObject>) field.get(openmrsObject); field.setAccessible(previousFieldAccessibility); return childCollection; } else { Class<? extends OpenmrsObject> openmrsObjectClass = openmrsObject.getClass(); Method getterMethod = openmrsObjectClass.getMethod(getterName, (Class[]) null); return (Collection<OpenmrsObject>) getterMethod.invoke(openmrsObject, new Object[] {}); } } catch (IllegalAccessException e) { if (field.isAnnotationPresent(AllowDirectAccess.class)) { throw new APIException("unable.get.field", new Object[] { fieldName, openmrsObject.getClass() }); } else { throw new APIException("unable.getter.method", new Object[] { "use", getterName, fieldName, openmrsObject.getClass() }); } } catch (InvocationTargetException e) { throw new APIException("unable.getter.method", new Object[] { "run", getterName, fieldName, openmrsObject.getClass() }); } catch (NoSuchMethodException e) { throw new APIException("unable.getter.method", new Object[] { "find", getterName, fieldName, openmrsObject.getClass() }); } }  @Override @SuppressWarnings("unchecked") void before(Method method, Object[] args, Object target); static void recursivelyHandle(Class<H> handlerType, OpenmrsObject openmrsObject,
	        String reason); @SuppressWarnings("unchecked") static void recursivelyHandle(Class<H> handlerType, OpenmrsObject openmrsObject,
	        User currentUser, Date currentDate, String other, List<OpenmrsObject> alreadyHandled);  }### Answer:
@Test public void getChildCollection_shouldGetValueOfGivenChildCollectionOnGivenField() throws Exception { MiniOpenmrsObject oo = new MiniOpenmrsObject(); List<Location> locs = new ArrayList<>(); Location location = new Location(1); locs.add(location); oo.setLocations(locs); Collection<OpenmrsObject> fetchedLocations = RequiredDataAdvice.getChildCollection(oo, MiniOpenmrsObject.class .getDeclaredField("locations")); Assert.assertTrue(fetchedLocations.contains(location)); }
@Test public void getChildCollection_shouldShouldBeAbleToGetAnnotatedPrivateFields() throws Exception { MiniOpenmrsObject oo = new MiniOpenmrsObject(); oo.setLocations(new ArrayList<>()); Assert.assertNotNull(RequiredDataAdvice .getChildCollection(oo, MiniOpenmrsObject.class.getDeclaredField("locations"))); }
@Test(expected = APIException.class) public void getChildCollection_shouldThrowAPIExceptionIfGetterMethodNotFound() throws Exception { ClassWithBadGetter oo = new ClassWithBadGetter(); oo.setMyLocations(new HashSet<>()); RequiredDataAdvice.getChildCollection(oo, ClassWithBadGetter.class.getDeclaredField("locations")); }