ArgumentMatchers.any must not be null

Getting matchers to work with Kotlin can be a problem.
If you have a method written in kotlin that does not take a nullable parameter, then we cannot match with it using Mockito.any().
This is because it can return void and this is not assignable to a non-nullable parameter.

If the method being matched is written in Java, then I think that it will work as all Java objects are implicitly nullable.

One possible solution would be to use a library like mockito-kotlin
But you can solve this issue easily with a few lines of code yourself.

If you need typed any(type: Class)

private fun <T> any(type: Class<T>): T = Mockito.any<T>(type)

OR

You can use this matcher instead of Matchers.any() :

object MockitoHelper {
fun <T> anyObject(): T {
    Mockito.any<T>()
    return uninitialized()
}
@Suppress("UNCHECKED_CAST")
   fun <T> uninitialized(): T = null as T
}

and use MockitoHelper.anyObject() instead of any() in your kotlin tests.

You can find more information in this post: Using Mockito with Kotlin

There is a discussion about possible solutions in this post :
Is it possible to use Mockito in Kotlin?

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)