How to match null passed to parameter of Class<T> with Mockito

blank picture blank · Oct 3, 2012 · Viewed 71.1k times · Source

I have methods like these:

public <T> method(String s, Class<T> t) {...}

That I need to check that null is passed to the second argument when using matchers for the other parameters, I have been doing this :

@SuppressWarnings("unchecked")
verify(client).method(eq("String"), any(Class.class));

But is there a better way (without suppress warnings) ? T represents the return type of some other method, which is sometimes void and in these cases null is passed in.

Answer

Dawood ibn Kareem picture Dawood ibn Kareem · Oct 3, 2012

Mockito has an isNull matcher, where you can pass in the name of the class. So if you need to use it with other matchers, the correct thing to do is

verify(client).method(eq("String"),isNull(Class<?>.class));

This is now deprecated, see the answer below for the new method - https://stackoverflow.com/a/41250852/1348