Search code examples
javagenericswarningsmockitomatcher

Mockito: List Matchers with generics


Mockito offers:

when(mock.process(Matchers.any(List.class)));

How to avoid warning if process takes a List<Bar> instead?


Solution

  • For Java 8 and above, it's easy:

    when(mock.process(Matchers.anyList()));
    

    For Java 7 and below, the compiler needs a bit of help. Use anyListOf(Class<T> clazz):

    when(mock.process(Matchers.anyListOf(Bar.class)));