Jesse
Jesse

Reputation: 525

Java Mockito- how to mock uncertain number of parameter method

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!");
}

Upvotes: 6

Views: 688

Answers (1)

Ryuzaki L
Ryuzaki L

Reputation: 40048

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

Upvotes: 4

Related Questions