Mockito argument matcher that matches any of a type with generics, excluding nulls

Viewed 282

Lets say I've got a class like this

public class Wrapper<T> {
    T data;
}

And I have a method call like this:

public interface ThingDoer {
     <T> boolean doSomething(Wrapper<T> wrapper)
}

which I want to mock out in a test. Lets say we're all set up with the mockito stuff, and now I'm trying to mock out this method call

when(thingDoer.doSomething(any(Wrapper.class))).thenReturn(true);

However, this will give me a warning: Unchecked assignment: 'package.Wrapper' to 'package.Wrapper<T>'

I read another suggestion somewhere that with Java 8, you're supposed to use any() instead of any(Wrapper.class). However, reading through the documentation on these two methods, any() will accept null arguments and any(Class) will reject null arguments, so they are not quite synonymous. Is there a way to exclude nulls without getting a warning?

1 Answers

Use isNotNull() as the matcher.

when(thingDoer.doSomething(isNotNull())).thenReturn(true);
Related