I am trying to write a Junit test which will verify whether the following method is called:
public long executeRequest(@RequestCodes.Code.RequestAnnotation int requestCode, Object requestInformation, RequestListener requestListener) {
boolean success = false;
... do stuff ...
return success ? 1L : -1L;
}
in a test using:
Mockito.when(mockedRequest.executeRequest(Matchers.any(RequestCodes.Code.RequestAnnotation.class), Matchers.any(RequestWrapper.class), Matchers.any(RequestListener.class))).thenReturn(1L);
The RequestCodes.Code.RequestAnnotation class is a elementary indef interface using an int to identify the call to make using a switch. Pretty much like this.
Matchers.any(RequestCodes.Code.RequestAnnotation.class)
won't work here and I have tried Matchers.any()
, Matchers.anyInt()
, Matchers.isA(RequestCodes.Code.RequestAnnotation.getClass())
(as well as anything else that came to mind) with no success.
Any suggestions would be much appreciated.
Mockito allows us to create mock objects and stub the behavior for our test cases. We usually mock the behavior using when() and thenReturn() on the mock object.
What are Matchers? Matchers are like regex or wildcards where instead of a specific input (and or output), you specify a range/type of input/output based on which stubs/spies can be rest and calls to stubs can be verified. All the Mockito matchers are a part of 'Mockito' static class.
Since Mockito any(Class) and anyInt family matchers perform a type check, thus they won't match null arguments.
refEq(T value, String... excludeFields) Object argument that is reflection-equal to the given value with support for excluding selected fields from a class.
For now, you can suppress this error using @SuppressWarnings("WrongConstant")
for this specific test. It works fine, and keeps your production clean.
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