Reputation: 305
I'm converting from Java to Kotlin, and from Mockito to MockK.
I'm stuck at converting Argument Matchers from Mockito to MockK.
Mockito can do any()
to match anything, including nulls and varargs. (imports ArgumentMatchers.any)
For example:
verify(object1).store(any(SomeClass.class));
Does MockK have anything for that? In this specific situation, it's not any primitive type. I'm trying to match a class Object.
Thank you!
Upvotes: 3
Views: 4941
Reputation: 332
In the case of migrate of Mockito to Mockk, consider the following:
With Mockito is encapsuled the class inside verify method, for example:
verify(object1).store(any(SomeClass.class));
In Mockk use lambda with receiver, similar to apply function (but without return), for example:
verify{ object1.store(any<SomeClass::class>()) }
And response your question, for specify the type can use any<YourType::class>, although the compiler marks it as unnecessary code because it has type inference, This is mainly useful when you have overloaded a function, so you can differentiate which parameters to receive for each one, for example:
class YourClass {
fun someMethod(value: String) {
}
fun someMethod(value: Int) {
}
}
fun test() {
val mock: YourClass = mockk()
verify { mock.someMethod(any<String>()) }
verify { mock.someMethod(any<Int>()) }
}
Upvotes: 1
Reputation: 2190
If you want to match a specific type, you could do
verify { object1.store(ofType(SomeClass::class)) }
Upvotes: 4
Reputation: 2742
In mockk, you can any()
like this.
verify { object1.store(any()) }
Upvotes: 0