### Question:
ReflectionUtil { public static Object invokeMethod(Object target, String methodName) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Class<?> cl = target.getClass(); int i = 0; while ((i++) < MAX_NEST_DEPTH && cl != null) { try { Method m = cl.getDeclaredMethod(methodName); m.setAccessible(true); return m.invoke(target); } catch (Exception e) { cl = cl.getSuperclass(); } } throw new NoSuchMethodException("class:" + target.getClass() + ", methodName:" + methodName); }  static Class<?> getClassByName(String className); static Object getFieldValue(Object target, String fieldName); static Object invokeMethod(Object target, String methodName); static Object invokeMethod(Object target, String methodName, Class<?>[] parameterTypes, Object[] args); static Object invokeStaticMethod(Class<?> targetClass, String methodName, Class<?>[] parameterTypes,
                                            Object[] parameterValues); static Method getMethod(Class<?> classType, String methodName, Class<?>[] parameterTypes); static Set<Class<?>> getInterfaces(Class<?> clazz); static void modifyStaticFinalField(Class cla, String modifyFieldName, Object newValue); static final int MAX_NEST_DEPTH; }### Answer:
@Test public void testInvokeMethod() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Assertions.assertEquals(0, ReflectionUtil.invokeMethod("", "length")); Assertions.assertEquals(3, ReflectionUtil.invokeMethod("foo", "length")); Assertions.assertThrows(NoSuchMethodException.class, () -> ReflectionUtil.invokeMethod("", "size")); }
@Test public void testInvokeMethod2() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Assertions.assertEquals(0, ReflectionUtil .invokeMethod("", "length", null, null)); Assertions.assertEquals(3, ReflectionUtil .invokeMethod("foo", "length", null, null)); Assertions.assertThrows(NoSuchMethodException.class, () -> ReflectionUtil .invokeMethod("", "size", null, null)); }