68

With Mockito I can do the following:

verify(someService).process(any(Person.class));

But how do I write this if process takes a Collection<Person> instead? Can't figure out how to write it correctly. Just getting syntax errors...

Svish
  • 152,914
  • 173
  • 462
  • 620

5 Answers5

111

Try:

verify(someService).process(ArgumentMatchers.<Collection<Person>>any());

Actually, IntelliJ automatically suggested this fix when I typed any()... Unfortunately you cannot use static import in this case.

Bob
  • 5,510
  • 9
  • 48
  • 80
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • 4
    Thanks it works... but God it looks horrible, there have to be a nicer way to right that... – Cristiano Fontes Jan 10 '13 at 19:01
  • 1
    Thumbs up on the only solution that answers the question as stated as opposed to targeting the special case of matching containers. – Adam Parkin Nov 15 '13 at 04:07
  • 1
    This answer is now incorrect. The answer below gives a better solution. – scubbo Oct 26 '15 at 18:58
  • 1
    I may be mistaken but I do not think this is the correct answer. Although it removes the warning, the verification switches from "any(Collection.class)" to any(), which does not verify anything. At least before there was a class check. Isn't it? – Gabriel Falcone Mar 16 '17 at 23:06
  • How did you get intellij to automatically suggest that to you? – Daniel Kaplan May 05 '17 at 21:59
  • 3
    `Matchers` is deprecated, should now use `ArgumentMatchers`. – Ray Aug 12 '19 at 16:54
27

Try :

verify(someService).process(anyCollectionOf(Person.class));

Since version 1.8 Mockito introduces

public static <T> Collection<T> anyCollectionOf(Class<T> clazz);
seblm
  • 370
  • 3
  • 6
  • For others who come to this and need it, there's also an anyListOf() in addition to anyCollectionOf(), see: http://stackoverflow.com/a/10512526/908677 – Elijah Lofgren Aug 16 '16 at 18:47
  • 1
    anyCollectionOf(Class clazz) will be removed in Mockito 3.0 and java8. There will be a new method: anyOptional(Class class) see: https://github.com/mockito/mockito/issues/308 – Naxos84 Jan 09 '18 at 11:15
1

if you use a own method, you can even use static import:

private Collection<Person> anyPersonCollection() {
    return any();
}

Then you can use

verify(someService).process(anyPersonCollection());
fxa
  • 11
  • 1
0

As an alternative to the accepted answer you can try:

verify(someService).process(Mockito.<SomeGenericClass<Person>>any());

Where I used org.mockito.Mockito instead of Matchers.

Oleksandr Tarasenko
  • 587
  • 1
  • 8
  • 16
-1

You can't express this because of type erasure. Even if you could express it in code, Mockito had no chance to check it at runtime. You could create an interface like

interface PersonCollection extends Collection<Person> { /* nothing */ }

instead and use this throughout your code.

Edit: I was wrong, Mockito has anyCollectionOf(..) which is what you want.

Waldheinz
  • 10,399
  • 3
  • 31
  • 61
  • 1
    Actually, it *could* be expressed with a "type literal" object if the API accepted it: `any(new TypeLiteral>() {})`. Not pretty, of course, but it does work since all the type information is available at runtime (through Reflection or a bytecode library). – Rogério May 30 '11 at 11:49
  • @Rogerio: you are right, and it seems Mockito indeed supports this now. Haven't used it in a while... – Waldheinz May 30 '11 at 11:56