Davide Giuseppe Farella
11/29/2022, 2:29 PMsealed interface A {
data class B(...): A
data class C(...): A
}
fun something(a: A): Int
every { something(any()) } returns 0
every { something(any<B>() } returns 1
I would expect that something(B(...))
would return 1 ✅ correct
And that something(C(...))
would return 0 ❌ wrong
This is because the Type
in any<Type>()
actually doesn’t “work”.
The correct way to do this is the more verbose one
every { something(any()) } answer {
when (firstArg<A>()) {
is B -> 1
else -> 0 // or error() or whatever
}
}
Is this expected or is it a bug?
Mock 1.13.2 on JVM 11Sam
11/29/2022, 2:33 PMofType<Type>()
?
every { something(ofType<B>()) } returns 1
Sam
11/29/2022, 2:34 PMany<Type>()
doesn’t actually check the type 😞)Davide Giuseppe Farella
11/29/2022, 2:36 PMofType
does work! Thank you so much!Klitos Kyriacou
11/29/2022, 3:52 PMany<Type>()
is still useful, though. The explicit generic parameter is required for compile-time overload resolution.