https://kotlinlang.org logo
#mockk
Title
# mockk
t

thanksforallthefish

10/23/2020, 9:45 AM
I might already have asked this, but forgot the answer (or never asked)
Copy code
@Test // <- junit5 annotation
fun publisher() {
    val publisher = mockk<Publisher>(relaxed = true)
    UseCase(publisher).doSomething()
    verify { publisher.publish(any<UseCase.Something>()) }
    verify { publisher.publish(any<String>()) }
}
interface Publisher {
    fun publish(any: Any)
    fun publish(event: Event) {
        publish(event as Any)
    }
    class Event(source: Any)
}
class UseCase(val publisher: Publisher) {
    fun doSomething() {
        publisher.publish(Something(""))
    }
    data class Something(val something: String)
}
the test passes. what am I not seeing?
j

jlw

10/27/2020, 3:28 PM
I think it is because
any
doesn’t pass the type info to the matcher. The type info is just there to satisfy the method parameter requirements:
Copy code
inline fun <reified T : Any> any(): T = match(ConstantMatcher(true))
Because both
UseCase.Something
and
String
are subs of
Any
, this test passes because it is called at least once.
2 Views