Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Mockito Matchers.any() with android.support.annotation.IntDef custom annotation

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.

like image 808
HOD Avatar asked Aug 02 '16 13:08

HOD


People also ask

What does Mockito any () do?

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 Mockito matchers?

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.

Does Mockito any () match null?

Since Mockito any(Class) and anyInt family matchers perform a type check, thus they won't match null arguments.

What is refEq?

refEq(T value, String... excludeFields) Object argument that is reflection-equal to the given value with support for excluding selected fields from a class.


1 Answers

For now, you can suppress this error using @SuppressWarnings("WrongConstant") for this specific test. It works fine, and keeps your production clean.

like image 63
Abdalrahman Shatou Avatar answered Oct 20 '22 01:10

Abdalrahman Shatou