Mockito.any() pass Interface with Generics

JavaGenericsMockito

Java Problem Overview


is it possible to pass the type of an interface with generics?

The interface:

public interface AsyncCallback<T>

In my test method:

Mockito.any(AsyncCallback.class)

Putting <ResponseX> behind or for .class didnt work.

Java Solutions


Solution 1 - Java

There is a type-safe way: use ArgumentMatchers.any() and qualify it with the type:

ArgumentMatchers.<AsyncCallback<ResponseX>>any()

Solution 2 - Java

Using Java 8, you can simply use any() (assuming static import) without argument or type parameter because of enhanced type inference. The compiler now knows from the target type (the type of the method argument) that you actually mean Matchers.<AsyncCallback<ResponseX>>any(), which is the pre-Java 8 solution.

Solution 3 - Java

I had to adopt the following mechamism to allow for generics:

import static org.mockito.Matchers.any;
List<String> list = any();
when(callMyMethod.getResult(list)).thenReturn(myResultString);

Hope this helps someone.

Solution 4 - Java

Posting pierrefevrier comment as answer which might be useful if it present in a answer instead of comments.

With new versions of Mockito: (Matchers.<AsyncCallback<ResponseX>>any()

Solution 5 - Java

Further to thSoft's answer putting the qualified call to any() in method meant I could remove the qualification since the return type allowed inference:

private HashMap<String, String> anyStringStringHashMap() {
    return Matchers.any();
}

Solution 6 - Java

You can just cast it, adding suppress warnings if you like:

@SuppressWarnings("unchecked")    
AsyncCallback<ResponseX> callback = Mockito.any(AsyncCallback.class)

If Java allowed 'generic' generics they could have a method like this which is what you are looking for

private static <T, E> T<E> mock(Class<T<E>> clazz)

Solution 7 - Java

I had a similar problem using Spring Example:

Mockito.when(repo.findAll(Mockito.<Example<SrvReqToSupplierComment>>any()))
			.thenReturn(Lists.emptyList());

Here, you have to use qualification, b/c findAll method can take multiple types, like Sort and Iterable. You can also use Mockito.any(Example.class) of course with the type safety warning.

Solution 8 - Java

Using a qualified generics type with the no-argument any() method works (i.e. ArgumentMatchers.<AsyncCallback<ResponseX>>any()), but can get unwieldy for longer generics expressions. An alternative is to put a no-argument any() call in its own generic method, using the specific generic type as the return type:

private static <T> AsyncCallback<T> anyAsyncCallback() {
  return ArgumentMatchers.any()
}

Usage

Mockito.verify(mockObject).performCallback(any(), anyAsyncCallback())

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlrxwView Question on Stackoverflow
Solution 1 - JavathSoftView Answer on Stackoverflow
Solution 2 - JavahermanView Answer on Stackoverflow
Solution 3 - JavatheINtoyView Answer on Stackoverflow
Solution 4 - JavasagView Answer on Stackoverflow
Solution 5 - JavaJWGSView Answer on Stackoverflow
Solution 6 - JavaGarrett HallView Answer on Stackoverflow
Solution 7 - JavagagarwaView Answer on Stackoverflow
Solution 8 - JavaM. JustinView Answer on Stackoverflow