Tweaks Matchers.any family matchers behavior
See original GitHub issueThis issue is a follow up of the work started in #141 by @pimterry. Also here’s an extract of a message on this thread of the mailing list :
The origin of these methods is they come from anything i.e. anything matches, later for shortness and cast avoidance the aliases grew, but the API naming thus became inconsistent with what a human would expect. So this behavior is being changed in mockito 2 beta, to be precise here’s the status on these API in the version 2.0.5-beta :
any,anyObject,any(Class)won’t check anything (at first they were just aliases for anything and for cast avoidance),nullis a valid valueanyXlikeanyStringwill check the arg is notnulland that has the correct typeanyListwill check the argument is not null and aListinstanceanyListOf(and the likes) at the moment are just aliases to their non generic counter part likeanyList
Note this is work in progress (started here in #141), these new behavior can / will change in the beta phase. I’m especially wondering if the any family should allow null and if not do a type check. For example with these matchers :
any,anyObjectstay the same, they currently allownulland don’t have to do type check anywayany(Class)currently allowsnulland doesn’t do type check => allowsnulland if not checks for the given typeany<Collection>Ofcurrently doesn’t allownulland does a type check of the collection, not elements => allowsnull, if not checks collection type, if not empty checks element type
Maybe extend/create a symmetric isA family API that won’t allow any null arguments.
Issue Analytics
- State:
- Created 8 years ago
- Comments:32 (24 by maintainers)

Top Related StackOverflow Question
My preference is to have the following matchers (option 1?):
<T> T any()that accepts anything includingnull. I associate “any” to the “anything” in my world that includes null-values and instances. It is compact and suitable for the most common cases.<T> T anyObject()or<T> T notNull()rejectnullvalues opposite to any(). The name should indicate that null is rejected to avoid confusion about its behaviour.<T> T isA(Class<? extends T>)rejectnullvalues and all values that are not a subtype ofT, like instanceof.any*()andany(T)should be dropped cause they have different behaviour (as described before)+1 i agree, lets try this for the RC.