Skip to content

Issue with mocking value classes with coEvery #1073

@Syex

Description

@Syex

Prerequisites

Please answer the following questions for yourself before submitting an issue.

  • I am running the latest version
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed

Failure Information (for bugs)

I'm running into an edge case issue with value classes being used as mock answers for suspending methods that implement an interface with generics.

Consider the following interface with an implementing class using Kotlin Result (or any other value class)

interface Action<Params, ReturnType> {

    suspend fun execute(params: Params): ReturnType
}

class ResultTest : Action<Unit, Result<String>> {

    override suspend fun execute(params: Unit): Result<String> {
        TODO("Not yet implemented")
    }
}

and a simple test like

private val resultTest = mockk<ResultTest>()

@Test
fun test() = runTest {
    coEvery { resultTest.execute(Unit) } returns Result.success("abc")

    val result = resultTest.execute(Unit)
    assertTrue(result.isSuccess)
}

it fails with

class java.lang.String cannot be cast to class kotlin.Result

when calling resultTest.execute(Unit).


With a modification of the return type of the interface method execute from ReturnType to Result<ReturnType>:

interface Action<Params, ReturnType> {

    suspend fun execute(params: Params): Result<ReturnType>
}

class ResultTest : Action<Unit, String> {

    override suspend fun execute(params: Unit): Result<String> {
        TODO("Not yet implemented")
    }
}

the same test will pass.

The test will also pass with the first version of the interface if I remove the suspend keyword and replace coEvery with every.

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

  • MockK version: 1.13.4
  • Kotlin version: 1.8.10
  • JDK version: 11
  • JUnit version: 5.9.2
  • Type of test: unit test OR android instrumented test: Unit

Stack trace

class java.lang.String cannot be cast to class kotlin.Result (java.lang.String is in module java.base of loader 'bootstrap'; kotlin.Result is in unnamed module of loader 'app')
java.lang.ClassCastException: class java.lang.String cannot be cast to class kotlin.Result (java.lang.String is in module java.base of loader 'bootstrap'; kotlin.Result is in unnamed module of loader 'app')
	at de.memorian.wearos.marsrover.domain.action.ResultTestB.getResult-IoAF18A(MockkResultTest.kt:60)
	at de.memorian.wearos.marsrover.domain.action.MockkResultTest$test$1.invokeSuspend(MockkResultTest.kt:21)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions