Skip to content

[1.13.16] Unable to mock nullable value class return (non primative) from suspend method #1338

@tKe

Description

@tKe

The following test case

@JvmInline
private value class StringValue(val value: String)

class NullableValueFromSuspendTest {
    @Test
    fun `verify mock returning nullable value class from suspend function`() {
        class MockTarget {
            suspend fun value(): StringValue? {
                yield()
                return null
            }
        }

        val mock = mockk<MockTarget> {
            coEvery { value() } returns StringValue("hello")
        }

        val result = runBlocking { mock.value() }

        assertEquals("hello", result?.value)
    }
}

results in the following failure:

class io.mockk.core.StringValue cannot be cast to class java.lang.String (io.mockk.core.StringValue is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
java.lang.ClassCastException: class io.mockk.core.StringValue cannot be cast to class java.lang.String (io.mockk.core.StringValue is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
	at io.mockk.core.NullableValueFromSuspendTest$verify mock returning nullable value class from suspend function$result$1.invokeSuspend(NullableValueFromSuspendTest.kt:27)
...

This test passes against 1.13.14 and was likely introduced in #1332.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions