sealed 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
Copy code
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 11
solved 1
s
Sam
11/29/2022, 2:33 PM
Are you looking for
ofType<Type>()
?
Copy code
every { something(ofType<B>()) } returns 1
Sam
11/29/2022, 2:34 PM
(I agree, it can be confusing that
any<Type>()
doesn’t actually check the type 😞)
d
Davide Giuseppe Farella
11/29/2022, 2:36 PM
ofType
does work! Thank you so much!
k
Klitos Kyriacou
11/29/2022, 3:52 PM
any<Type>()
is still useful, though. The explicit generic parameter is required for compile-time overload resolution.