MethodCallMerger implements SinglePropertyValueMerger<Object, Object> { @Override public Object mergeObjects(Object value, String[] extraParam) { if (value == null) { return null; } if (ArrayUtils.isEmpty(extraParam)) { throw new IllegalArgumentException("You must provide a method to call as the first parameter."); } try { Class cls = value.getClass(); Class[] parms = new Class[0]; while(!cls.equals(Object.class)) { Method method = MethodUtils.getAccessibleMethod(cls, extraParam[0], parms); if (method == null) { cls = cls.getSuperclass(); continue; } return method.invoke(value); } throw new IllegalArgumentException("Method with name: "+extraParam[0]+" not found."); } catch (Exception ex) { logger.error("Error while trying to invoke method: "+extraParam[0], ex); throw new RuntimeException(ex); } }  @Override Object mergeObjects(Object value, String[] extraParam); @Override boolean isRestoreSupported(String[] params); @Override Object restoreObject(Object object, String[] params);  }
@Test public void testCallCollectionSize() { List<String> testValue = Arrays.asList("First", "Second"); Object expected = testValue.size(); String[] method = {"size"}; Object result = subject.mergeObjects(testValue, method); assertEquals("result should be the size of the collection.", expected, result); }
@Test(expected=RuntimeException.class) public void testUnexistentMethod() { String[] method = {"nonSenseMethod"}; subject.mergeObjects(this, method); }