I try to use Mockito to mock the getDeclaredMethod()
of java.
but the parameter of this method is un-certain. how to mock such method?
public Method getDeclaredMethod(String name, Class... parameterTypes) throws NoSuchMethodException, SecurityException {
throw new RuntimeException("Stub!");
}
Use ArgumentMatchers.any()
Matches anything, including nulls and varargs.
Example
when(mockedObject.getDeclaredMethod(anyString(),any())).thenReturn("element");
In your case
when(mockedObject.getDeclaredMethod(anyString(), (Class<?>)any())).thenReturn("element");
And also anyVararg() but which is Deprecated. as of 2.1.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With