Skip to content

Using coEvery on functions that return kotlin.Result fail with "class kotlin.Result cannot be cast" in 1.13.16 #1351

@stiless-udf

Description

@stiless-udf

This works in 1.13.14 but using 1.13.16 any function that returns a kotlin.Result mocked using coEvery fails with an exception that class kotlin.Result cannot be cast.

Below is a sample where it happens. If I run the test here I get the following error:
class kotlin.Result cannot be cast to class java.lang.String

This same test passes with 1.13.14

interface InterfaceA {
    suspend fun doSomething(): Result<String>
}

interface InterfaceB {
    suspend fun onSuccess()
    suspend fun onFailure()
}

class ClassA(
    private val interfaceA: InterfaceA,
    private val interfaceB: InterfaceB
) {
    suspend fun testFunc() {
        interfaceA.doSomething()
            .onSuccess { interfaceB.onSuccess() }
            .onFailure { interfaceB.onFailure() }
    }
}

@Test
    fun `test result`() = runTest {
        val interfaceA = mockk<InterfaceA>()
        val interfaceB = mockk<InterfaceB>()
        coEvery { interfaceA.doSomething() } returns Result.success("hello")
        coEvery { interfaceB.onSuccess() } just runs
        coEvery { interfaceB.onFailure() } just runs
        val classA = ClassA(
            interfaceA = interfaceA,
            interfaceB = interfaceB
        )

        classA.testFunc()

        coVerify { interfaceB.onSuccess() }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions