Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matchers.any() for null value in Mockito

Tags:

java

mockito

Suppose I am having this object objectDemo which calls to the method objectDemoMethod with 2 parameters String and null. Now I want to verify with Mockito that this method was called:

objectDemo.objectDemoMethod("SAMPLE_STRING", null); 

I have written this:

Mockito.verify(objectDemo, Mockito.times(1)).objectDemoMethod(Matchers.any(String.class), null); 

but it's giving an error:

Invalid use of argument matchers for null value.

Is there any another way to pass null value?

like image 811
Avinash Jadhav Avatar asked Sep 29 '15 10:09

Avinash Jadhav


People also ask

Does Mockito any () match null?

Since Mockito any(Class) and anyInt family matchers perform a type check, thus they won't match null arguments.

Can any return null?

I've just realised the any() method does return null for these cases. It returns the default value for the class so null for all objects and the default primitive value for primitive wrapper classes like Integer. So it isn't wrong that these methods return null.

What are matchers in Mockito?

What are Matchers? Matchers are like regex or wildcards where instead of a specific input (and or output), you specify a range/type of input/output based on which stubs/spies can be rest and calls to stubs can be verified. All the Mockito matchers are a part of 'Mockito' static class.

Can the Mockito Matcher methods be used as return values?

Matcher methods can't be used as return values; there is no way to phrase thenReturn(anyInt()) or thenReturn(any(Foo. class)) in Mockito, for instance. Mockito needs to know exactly which instance to return in stubbing calls, and will not choose an arbitrary return value for you.


2 Answers

The error message you are getting is expected since you are using argument matcher for only one argument and not the other. From Matchers Javadoc:

If you are using argument matchers, all arguments have to be provided by matchers.

Therefore, the fix is to use a matcher for the second parameter of the method as well. In this case, it would be a matcher matching null. Depending on the version of Mockito and Java, you can have:

  • Starting with Mockito 2, you can use ArgumentMatchers.isNull(). This works with Java 8 and above:

    verify(objectDemo, times(1)).objectDemoMethod(any(String.class), isNull()); 

    Note that if you're running with Java 7 or older, you'll need an explicit cast to make this work, because the type inference in those versions of Java does not take into account the types of the method called:

    verify(objectDemo, times(1)).objectDemoMethod(any(String.class), (String) isNull()); 
  • If you're using Mockito 1, you can use the Matchers.isNull(clazz) instead:

    verify(objectDemo, times(1)).objectDemoMethod(any(String.class), isNull(String.class)); 

For the Java ≤ 7 or Mockito 1 cases, the examples uses a case where the second parameter was of type String: it would need to be replaced with the actual type of the method parameter.

like image 81
Tunaki Avatar answered Oct 08 '22 05:10

Tunaki


isNull seems to be deprecated

With Java 8 this method will be removed in Mockito 3.0. This method is only used for generic friendliness to avoid casting, this is not anymore needed in Java 8.

I think you could use nullable:

  • public static <T> T nullable(Class<T> clazz)

You could use something like:

verify(objectDemo, times(1)).objectDemoMethod(any(String.class), nullable(String.class)); 
like image 23
Amadeu Cavalcante Filho Avatar answered Oct 08 '22 05:10

Amadeu Cavalcante Filho