Endre Deak
06/03/2024, 2:53 PMinterface X<T: Comparable<T>> {
fun get(): T
}
@Test
fun `test x`() {
val x = mockk<X<Int>>()
every { x.get() } returns 1 # fails
every { x.get().hint(Int::class) } returns 1 # fails
val result = x.get()
result shouldBe 1
verify(exactly = 1) {
x.get()
}
}
# workaround:
interface TypedX : X<Int>
@Test
fun `test x`() {
val x = mockk<TypedX>()
every { x.get() } returns 1 # passes
val result = x.get()
result shouldBe 1
verify(exactly = 1) {
x.get()
}
}
The error I get:
Class cast exception happened.
Probably type information was erased.
In this case use `hint` before call to specify exact return type of a method.
io.mockk.MockKException: Class cast exception happened.
Probably type information was erased.
In this case use `hint` before call to specify exact return type of a method.
Caused by: java.lang.ClassCastException: class io.mockk.renamed.java.lang.Number$Subclass3 cannot be cast to class java.lang.Comparable (io.mockk.renamed.java.lang.Number$Subclass3 is in unnamed module of loader 'app'; java.lang.Comparable is in module java.base of loader 'bootstrap')
...