Skip to content

registerInstanceFactory supplier is not used for value classes #1199

@scmorse

Description

@scmorse

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

Steps to Reproduce

When using a value class with init {} validation, such as this example:

@JvmInline value class MyToken(val raw: String) {
  init { require(raw.startsWith("MY_PREFIX::")) }
}

Then a mock such as this

@Test
fun `should work with value classes`() {
  registerInstanceFactory {
    println("called")
    MyToken("MY_PREFIX::bar")
  }
  val userOfMyToken = mockk<UserOfMyToken>()
  every { userOfMyToken.useMyToken(any()) } returns Unit
}

interface UserOfMyToken {
  fun useMyToken(myToken: MyToken)
}

Then "called" is not logged, and the test fails with an IllegalArgumentException on the init's require().

But if I just change the value class to a data class:

data class MyToken(val raw: String) {
  init { require(raw.startsWith("MY_PREFIX::")) }
}

Then "called" is logged, and the the test passes. And using withInstanceFactory instead of registerInstanceFactory produces the same result.

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