Mockito + Kotlin: How to mock a value class return (inline class)

Viewed 980

recently I changed a piece of code from a typealias into a value class to perform type safety and at the same time have extension functions.

When adapting some tests to comply with the change I found out that I am unable to mock the response of a method if the data is the value class. The framework complains with the following:

java.lang.ClassCastException: class com.king.uplevelmanager.util.Base64Data cannot be cast to class java.lang.String (com.king.uplevelmanager.util.Base64Data is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')

The code snipped that I am trying is the following (kind of)

Test:

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.`should be equal to`
import org.junit.jupiter.api.Test
import org.mockito.kotlin.argWhere
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever

class SimpleTest2 {

    private val instance = MockSuspendClassImpl()
    private val mockedInstance: MockSuspendClass = mock()

    @Test
    fun `test with corroutines simple - no mock`() {
        // GIVEN
        val data = Data("something")
        val unit = UnderTest(instance)

        // WHEN
        val ret = unit.doSomethingPlain(data)

        // THEN
        ret `should be equal to` data.data
    }

    @Test
    fun `test with corroutines data - no mock`() {
        // GIVEN
        val data = Data("something")
        val unit = UnderTest(instance)

        // WHEN
        val ret = unit.doSomethingValue(data)

        // THEN
        ret `should be equal to` data
    }

    @Test
    fun `test with corroutines simple - mock`() = runTest {
        // GIVEN
        val data = Data("something")
        val unit = UnderTest(mockedInstance)
        whenever(mockedInstance.printData(eqValueClass(data, { it.data }))) doReturn data.data

        // WHEN
        val ret = unit.doSomethingPlain(data)

        // THEN
        ret `should be equal to` data.data
    }

    @Test
    fun `test with corroutines data - mock`() = runTest {
        // GIVEN
        val data = Data("something")
        val unit = UnderTest(mockedInstance)
        whenever(mockedInstance.printDataReturn(eqValueClass(data, { it.data }))) doReturn data

        // WHEN
        val ret = unit.doSomethingValue(data)

        // THEN
        ret `should be equal to` data
    }
}

class UnderTest(val mockedClass: MockSuspendClass) {
    fun doSomethingPlain(data: Data): String {
        return runBlocking(Dispatchers.Default) { mockedClass.printData(data) }
    }

    fun doSomethingValue(data: Data): Data {
        return runBlocking(Dispatchers.Default) { mockedClass.printDataReturn(data) }
    }
}

interface MockSuspendClass {
    suspend fun printData(data: Data): String
    suspend fun printDataReturn(data: Data): Data
}

class MockSuspendClassImpl : MockSuspendClass {
    override suspend fun printData(data: Data): String {
        println("Data is $data")
        delay(10)
        return data.data
    }

    override suspend fun printDataReturn(data: Data): Data {
        println("Data is $data")
        delay(10)
        return data
    }
}

@JvmInline
value class Data(val data: String)

/**
 * Workaround to support matching of value classes
 *
 * From: https://github.com/mockito/mockito-kotlin/issues/445#issuecomment-983619131
 */
inline fun <Outer, reified Inner> eqValueClass(
    expected: Outer,
    crossinline access: (Outer) -> Inner,
    test: ((actual: Any) -> Boolean) -> Any? = ::argWhere
): Outer {
    val assertion: (Any) -> Boolean = { actual ->
        if (actual is Inner) {
            access(expected) == actual
        } else {
            expected == actual
        }
    }
    @Suppress("UNCHECKED_CAST")
    return test(assertion) as Outer? ?: expected
}

Note that I probably do not need that custom matcher but that at least makes sure that the matcher works.

I have tested changing the method to return the primitive (String) and works properly. Am I missing something? Might the Mockito framework not support this? (strange given this: https://github.com/mockito/mockito/blob/main/src/main/java/org/mockito/internal/util/KotlinInlineClassUtil.java)

Thanks for any hint!

0 Answers
Related