236

I have an interface with a method that expects an array of Foo:

public interface IBar {
  void doStuff(Foo[] arr);
}

I am mocking this interface using Mockito, and I'd like to assert that doStuff() is called, but I don't want to validate what argument are passed - "don't care".

How do I write the following code using any(), the generic method, instead of anyObject()?

IBar bar = mock(IBar.class);
...
verify(bar).doStuff((Foo[]) anyObject());

5 Answers 5

301

This should work

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;

verify(bar).DoStuff(any(Foo[].class));
4
  • 31
    just in case someone need it in Scala: verify(bar).DoStuff(any[Array[Foo]])
    – tolitius
    Commented May 29, 2012 at 22:47
  • 7
    I had a problem with the import, I was using the any() from hamcrest in my imports and it collided with the one from mockito. Commented Jun 13, 2014 at 13:55
  • 4
    Please have a look into the API, the class argument is just used for casting, the method still accepts any kind of object! site.mockito.org/mockito/docs/current/org/mockito/…. Use isA() for this case site.mockito.org/mockito/docs/current/org/mockito/….
    – thilko
    Commented Aug 31, 2015 at 11:54
  • 4
    This class is now deprecated in order to avoid a name clash with Hamcrest. Use org.mockito.ArgumentMatchers
    – leo9r
    Commented Mar 22, 2019 at 23:26
149

Since Java 8 you can use the argument-less any method and the type argument will get inferred by the compiler:

verify(bar).doStuff(any());

Explanation

The new thing in Java 8 is that the target type of an expression will be used to infer type parameters of its sub-expressions. Before Java 8 only arguments to methods where used for type parameter inference (most of the time).

In this case the parameter type of doStuff will be the target type for any(), and the return value type of any() will get chosen to match that argument type.

This mechanism was added mainly to be able to compile lambda expressions, but it improves type inferences generally.


Primitive types

This doesn't work with primitive types, unfortunately:

public interface IBar {
    void doPrimitiveStuff(int i);
}

verify(bar).doPrimitiveStuff(any()); // Compiles but throws NullPointerException
verify(bar).doPrimitiveStuff(anyInt()); // This is what you have to do instead

The problem is that the compiler will infer Integer as the return value type of any(). Mockito will not be aware of this (due to type erasure) and return the default value for reference types, which is null. The runtime will try to unbox the null return value by calling the intValue method on it before passing it to doStuff, and the exception gets thrown.

2
  • I am pleasantly surprised every time this answer gets an upvote! I would have guessed that the question would not attract much attention since Java 8, since the any method should just work. You don't lookup answer for things that just work!
    – Lii
    Commented May 12, 2020 at 6:54
  • 1
    I came here because I didn't know why my code did not work with any() but was ok with anyBoolean(), which the last part of your answer sheds light on beautifully.
    – AdrienW
    Commented Jun 22, 2020 at 6:34
16

As I needed to use this feature for my latest project (at one point we updated from 1.10.19), just to keep the users (that are already using the mockito-core version 2.1.0 or greater) up to date, the static methods from the above answers should be taken from ArgumentMatchers class:

import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.ArgumentMatchers.any;

Please keep this in mind if you are planning to keep your Mockito artefacts up to date as possibly starting from version 3, this class may no longer exist:

As per 2.1.0 and above, Javadoc of org.mockito.Matchers states:

Use org.mockito.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.

I have written a little article on mockito wildcards if you're up for further reading.

3
  • How can I import org.mockito.ArgumentMatcher in Scala? I tried import org.mockito.ArgumentMatcher.any I get error `value any is not a member of object org.mockito.ArgumentMatcher Commented Mar 8, 2019 at 13:30
  • Could you please tell me what is the equivalent in version 3.0? Commented Mar 8, 2019 at 13:31
  • 1
    We shall know once it will be released ;) Commented Jun 4, 2019 at 6:32
13

You can use Mockito.isA() for that:

import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.verify;

verify(bar).doStuff(isA(Foo[].class));

http://site.mockito.org/mockito/docs/current/org/mockito/Matchers.html#isA(java.lang.Class)

3
  • This is the correct answer. Using any(Clazz) is completely wrong. Commented Apr 27, 2017 at 13:08
  • 4
    @SurasinTancharoen Actually, any(Class) is just an alias of isA(Class) (see the docs). So it's not at all wrong.
    – jmiserez
    Commented Dec 6, 2017 at 10:29
  • isA is deprecated Commented Sep 24, 2020 at 5:44
0

In the recent version of Mockito you could use any(Foo.class)

Not the answer you're looking for? Browse other questions tagged or ask your own question.