Mockito: List Matchers with generics

Philippe Blayo picture Philippe Blayo · May 9, 2012 · Viewed 133.1k times · Source

Mockito offers:

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

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

Answer

artbristol picture artbristol · May 9, 2012

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)));