Seems like you have two options here:
Accept anything
You can use any()
or notNull()
, as geoand mentioned in the comments. These define behavior for any parameter (any
) or any non-null parameter (notNull
).
when(api.mockedMethod(Mockito.any()).thenReturn(3L);
// or
when(api.mockedMethod(Mockito.notNull()).thenReturn(3L);
These static methods return argument matchers, and can be found on the Mockito object inherited from the Matchers object (to keep the APIs a little bit separated).
Write your own custom matcher
If you need to check that the object matches some custom criteria, you could use a custom ArgumentMatcher object. These objects (otherwise known as Hamcrest matchers, as they come directly from the Hamcrest library) have exactly one boolean method, that returns true if the object matches the condition.
/** Returns true if the passed object has FooBar anywhere in its class name. */
class HasFooBarInClassName extends ArgumentMatcher<Object> {
@Override public boolean matches(Object object) {
return object.getClass().getName().contains("FooBar");
}
}
when(api.mockedMethod(argThat(new HasFooBarInClassName())).thenReturn(3L);
Bonus: Capturing the Object
Sometimes your code will need to "capture" the object for additional verification or processing, in a way that Mockito can't do alone. You can use an ArgumentCaptor
to help you out there:
ArgumentCaptor<Object> captor = ArgumentCaptor.for(Object.class);
when(api.mockedMethod(any()).thenReturn(3L);
testedMethod();
verify(api).mockedMethod(captor.capture());
// Now you can check the object, as needed, using your own methods.
checkObjectAsNeeded(captor.getValue());