203

Mockito offers:

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

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

juankysmith
  • 11,839
  • 5
  • 37
  • 62
Philippe Blayo
  • 10,610
  • 14
  • 48
  • 65

4 Answers4

321

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)));
artbristol
  • 32,010
  • 5
  • 70
  • 103
  • 23
    Note: this is deprecated in Mockito 2.* and will be removed in Mockito 3. Deprecated because Java 8 compiler can infer the type now. – Kip Nov 29 '16 at 00:02
  • @artbristol do you know if with anySet() should work as same as anyList()? I am in Java 8 and a warning is thrown in Eclipse IDE – Fernando Fradegrada Jul 14 '17 at 13:47
  • 2
    Better to use `anyListOf`. Even though `anyList` works, it emits a warning. – balki Oct 26 '17 at 00:37
  • 8
    `anyListOf` is deprecated, so it is better NOT to use it. Example for Java 8 doesn't work in case of method overload, for example if you have a method accepting 2 different lists: `List` and `List` I've solved this problem using `ArgumentMatchers` with generic: `when(adapter.adapt(ArgumentMatchers.anyList())).thenCallRealMethod();` – edufinn Jul 08 '19 at 11:11
27

In addition to anyListOf above, you can always specify generics explicitly using this syntax:

when(mock.process(Matchers.<List<Bar>>any(List.class)));

Java 8 newly allows type inference based on parameters, so if you're using Java 8, this may work as well:

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

Remember that neither any() nor anyList() will apply any checks, including type or null checks. In Mockito 2.x, any(Foo.class) was changed to mean "any instanceof Foo", but any() still means "any value including null".

NOTE: The above has switched to ArgumentMatchers in newer versions of Mockito, to avoid a name collision with org.hamcrest.Matchers. Older versions of Mockito will need to keep using org.mockito.Matchers as above.

Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
  • 4
    ```Matchers.any()``` is very convenient! – MBach May 12 '16 at 08:58
  • Matchers is now deprecated, here's the info from mockito "Use ArgumentMatchers. This class is now deprecated in order to avoid a name clash with Hamcrest org.hamcrest.Matchers class. This class will likely be removed in version 3.0." https://static.javadoc.io/org.mockito/mockito-core/2.7.21/org/mockito/Matchers.html – oddmeter Apr 04 '17 at 18:51
  • @oddmeter Changes made. – Jeff Bowman Apr 04 '17 at 18:59
16

Before Java 8 (versions 7 or 6) I use the new method ArgumentMatchers.anyList:

import static org.mockito.Mockito.*;
import org.mockito.ArgumentMatchers;

verify(mock, atLeastOnce()).process(ArgumentMatchers.<Bar>anyList());
Alan Teals
  • 451
  • 4
  • 8
1

I needed anyList() with a typed ArrayList, the following worked:

(ArrayList<Bar>) ArgumentMatchers.<Bar>anyList()
Dominik Ehrenberg
  • 1,533
  • 1
  • 15
  • 12