apomelov
03/07/2018, 11:37 AMany(Class)
argument matcher. Shortcuts like anyString
or anyInt
work. Even works with primitives like so: doNothing().when(mock).test(any(Int::class.java)
. But If I change Int
in this sample to smth more valuable, e.g. BigInteger, it fails with
You cannot use argument matchers outside of verification or stubbing.I'm mocking an interface with spring's MockBean annotation. What am I doing wrong?
lovis
03/07/2018, 12:10 PMany
or anyObject
return null
, which is probably not what you expect, while anyInt
returns an Int
(I think -1, but I’m not sure)lovis
03/07/2018, 12:16 PMapomelov
03/07/2018, 12:18 PMThis message may appear after an NullPointerException if the last matcher is returning an object
like any() but the stubbed method signature expect a primitive argument, in this case,
use primitive alternatives.
when(mock.get(any())); // bad use, will raise NPE
when(mock.get(anyInt())); // correct usage use
apomelov
03/07/2018, 12:22 PMlovis
03/07/2018, 12:25 PMprivate fun <T> safeAny(): T {
return null as T
}
lovis
03/07/2018, 12:26 PMapomelov
03/07/2018, 12:27 PMapomelov
03/07/2018, 12:34 PMlovis
03/07/2018, 12:35 PMlovis
03/07/2018, 12:36 PMlovis
03/07/2018, 12:36 PMapomelov
03/07/2018, 12:37 PMlovis
03/07/2018, 12:38 PMapomelov
03/07/2018, 12:51 PM<T> any()
from mockito-kotlin instead of any(Class<T>)
from mockito itself.