Hi. Suddenly found out that I can't use mockito's ...
# announcements
a
Hi. Suddenly found out that I can't use mockito's
any(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?
l
problem is that
any
or
anyObject
return
null
, which is probably not what you expect, while
anyInt
returns an
Int
(I think -1, but I’m not sure)
hmm. no wait, that should actually give you a different error
a
Error description contains this:
Copy code
This 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
O! that's about nulls. I just changed signature of the tested method: replaced all types with nullable ones. Now it works.
l
Yes, but I guess you don’t want the signature change. So what you need is something like
Copy code
private fun <T> safeAny(): T {
    return null as T
}
or use https://github.com/nhaarman/mockito-kotlin I think it has this already
a
yeah, I already saw that lib.. thanks
only thing still is not clear: will I be able to integrate it with spring boot test runner. I specify my application context there and add mockbeans. It creates context, replacing some beans with mocks. Using mockito.
l
Not sure about that. Maybe ask in #C0B8ZTWE4
but the testrunner should’nt use any matchers by itself, should it?
it doesn’t assert anything
a
It creates the mock object
l
then it should be fine. the issue is with the matchers only
a
yes, it works. Just use
<T> any()
from mockito-kotlin instead of
any(Class<T>)
from mockito itself.
👍 1
508 Views