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),null
is a valid valueanyX
likeanyString
will check the arg is notnull
and that has the correct typeanyList
will check the argument is not null and aList
instanceanyListOf
(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
,anyObject
stay the same, they currently allownull
and don’t have to do type check anywayany(Class)
currently allowsnull
and doesn’t do type check => allowsnull
and if not checks for the given typeany<Collection>Of
currently doesn’t allownull
and 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 GitHub Comments
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()
rejectnull
values opposite to any(). The name should indicate that null is rejected to avoid confusion about its behaviour.<T> T isA(Class<? extends T>)
rejectnull
values 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.