I am trying to write a test where I am very simply checking if a method was called on a mock. Should be easy, I've done it many times using verify, however for some reason my test is failing because it seems to be comparing the actual invocation arguments to <any Type>
.
My code:
val mockSearchApi = mock<SearchApi>()
whenever(mockSearchApi.searchBrands(any(), any(), any(), any(), any(), any(), any(), any()))
.thenReturn(Single.just(getSearchApiResponse()))
// Some code that causes the api .searchBrands to get called
verify(mockSearchApi, times(1)).searchBrands(any(), any(), any(), any(), any(), any(), any(), any())
Then the test fails with error
Argument(s) are different! Wanted:
searchApi.searchBrands(
<any java.lang.Boolean>,
<any java.util.List>,
<any java.lang.Integer>,
<any java.lang.Integer>,
<any java.lang.String>,
<any java.lang.String>,
<any java.lang.Boolean>,
<any java.util.Map>
);
-> at ...
Actual invocations have different arguments:
searchApi.searchBrands(
false,
[],
1,
null,
null,
null,
true,
{}
);
So the method is called and the argument types are correct, so why does this fail?
And I have also tried changing all of those any() calls to anyBoolean(), anyList(), anyString(), etc. and the same happens. I have also ensured that the code is calling the arguments in the right order as I've seen that issue before with mocks as well.
Most likely you used any()
from mockito-kotlin
Original ArgumentMatchers
behaviour:
any()
- Matches anything, including nulls and varargs.any(Class<T> type)
- Matches any object of given type, excluding nulls.To prevent exception when null
is returned, mockito-kotlin changes this behaviour. See Matchers.kt
/** Matches any object, excluding nulls. */
inline fun <reified T : Any> any(): T {
return ArgumentMatchers.any(T::class.java) ?: createInstance()
}
/** Matches anything, including nulls. */
inline fun <reified T : Any> anyOrNull(): T {
return ArgumentMatchers.any<T>() ?: createInstance()
}
mockito-kotlin matchers behaviour:
any()
delegates to ArgumentMatchers.any(T::class.java)
and thus does not match nullsanyOrNull()
delegates to ArgumentMatchers.any<T>()
and thus matches null