-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
In mockito the any
and anyNamed
utilities allow flexible targeting for stub behavior and required being able to be passed to any argument position. Before null safety this was accomplished by giving them the value of null
which could be passed to any argument type. With null safety, non-nullable arguments can't use this technique. With codegen, the Mock*
subclass widens all argument types to be nullable. This requires that the type of an expression in a when
argument needs to be typed as the Mock*
class instead of the mocked type so at the position of the call passing any
the argument is statically nullable.
This isn't always obvious, and a potential mistake during a migration is to try to cast the any
to another type like when(someMock.someCall(any as int))
. This cast is guaranteed to fail when running with sound null safety.
We should consider a lint which warns when an expression with type Null
is cast to any non-nullable type, since that cast will always fail when running when sound null safety.