Skip to content

[1.13.13] ClassCastException when mocking function returning a nullable value class containing another value class #1308

@felixkrull-neuland

Description

@felixkrull-neuland

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

Expected Behavior

Can mock a function returning a nullable value class containing another value class

Current Behavior

ClassCastException when calling the mocked function returning aforementioned value class sandwich

Failure Information (for bugs)

This is a regression from 1.13.12 to .13 when returning nested value classes from a mocked function.

Steps to Reproduce

  1. have a value class
  2. have another value class containing the first value class
  3. have a function returning the second value class, nullable
  4. mock the function
  5. call the mocked function

Removing the nullability makes the problem go away, as does moving the nullability into either of the value classes.

Context

I'm guessing this is related to the fix for #813

Stack trace

Exception in thread "main" java.lang.ClassCastException: class Outer cannot be cast to class java.lang.String (Outer is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
	at Cls.returnValueClass-dzOYYKU(Repro.kt:11)
	at ReproKt.main(Repro.kt:17)
	at ReproKt.main(Repro.kt)

Minimal reproducible code (the gist of this issue)

// --------- Gradle deps
dependencies {
    implementation("io.mockk:mockk:1.13.13")
}

// --------- code ---------
import io.mockk.every
import io.mockk.mockk

@JvmInline
value class Inner(val underlying: String)

@JvmInline
value class Outer(val inner: Inner)

open class Cls {
    fun returnValueClass(): Outer? = null
}

fun main() {
    val mock = mockk<Cls>()
    every { mock.returnValueClass() } returns Outer(Inner("mock"))
    println(mock.returnValueClass())
}

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