Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

registerInstanceFactory supplier is not used for value classes #1199

Open
3 tasks done
scmorse opened this issue Jan 9, 2024 · 0 comments
Open
3 tasks done

registerInstanceFactory supplier is not used for value classes #1199

scmorse opened this issue Jan 9, 2024 · 0 comments

Comments

@scmorse
Copy link

scmorse commented Jan 9, 2024

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.

@scmorse scmorse changed the title registerInstanceFactory / withInstanceFactory suppliers are not used for value classes registerInstanceFactory supplier is not used for value classes Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant