@Test void invokeNoArgsWithResult() throws NoSuchMethodException { TestClass mock = Mockito.mock(TestClass.class); Optional<String> expectedResult = Optional.of("The result"); when(mock.method2()).thenReturn(expectedResult); Method method2 = mock.getClass().getDeclaredMethod("method2"); Object result = MethodUtil.invoke(method2, mock); Mockito.verify(mock, times(1)).method2(); assertThat(result, is(expectedResult)); }
public static Object invoke(Method method, Object target, Object... args) { try { method.setAccessible(true); return method.invoke(target, args); } catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } }
MethodUtil extends UtilityClass { public static Object invoke(Method method, Object target, Object... args) { try { method.setAccessible(true); return method.invoke(target, args); } catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } } }
MethodUtil extends UtilityClass { public static Object invoke(Method method, Object target, Object... args) { try { method.setAccessible(true); return method.invoke(target, args); } catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } }  }
MethodUtil extends UtilityClass { public static Object invoke(Method method, Object target, Object... args) { try { method.setAccessible(true); return method.invoke(target, args); } catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } }  static Object invoke(Method method, Object target, Object... args); static Stream<A> annotations(Class<A> annotationClass, Method method); static Method fromReference(Function1<T,V> methodReference); static Method fromReference(FunctionInt<T> methodReference); static Method fromReference(FunctionOptional1<T,V> methodReference); static Class<?> referringClass(Function1<T,V> methodReference); static Class<?> referringClass(FunctionInt<T> methodReference); static Class<?> referringClass(FunctionOptional1<T,V> methodReference); }
MethodUtil extends UtilityClass { public static Object invoke(Method method, Object target, Object... args) { try { method.setAccessible(true); return method.invoke(target, args); } catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } }  static Object invoke(Method method, Object target, Object... args); static Stream<A> annotations(Class<A> annotationClass, Method method); static Method fromReference(Function1<T,V> methodReference); static Method fromReference(FunctionInt<T> methodReference); static Method fromReference(FunctionOptional1<T,V> methodReference); static Class<?> referringClass(Function1<T,V> methodReference); static Class<?> referringClass(FunctionInt<T> methodReference); static Class<?> referringClass(FunctionOptional1<T,V> methodReference);  }