How to match a possible null parameter in Mockito
From the javadocs of any()
Since Mockito 2.1.0, only allow non-null
String
. As this is a nullable reference, the suggested API to matchnull
wrapper would beisNull()
. We felt this change would make tests harness much safer that it was with Mockito 1.x.
So, the way to match nullable string arguments is explicit declaration:
nullable(String.class)
I got this to work by switching to any(String.class)
I find this a bit misleading, because the API seems to suggest that anyString()
is just an alias for any(String.class)
at least up till the 2.0 update. To be fair, the documentation does specify that anyString()
only matches non-null strings. It just seems counter-intuitive to me.