0

Goal

I would like to globally disable inlining of @JvmInline value class classes via a compiler flag or something similar. I would want to do this when running unit tests but not in production.

Motivation

I would like to use mockk with value classes.

I want to write a unit test that looks like this:

  @JvmInline
  value class Example(private val inner: Int)

  class ExampleProvider {
    fun getExample(): Example = TODO()
  }

  @Test
  fun testMethod() {
    val mockExample = mockk<Example>()
    val mockProvider = mockk<ExampleProvider> {
      every { getExample() } returns mockExample
    }
    Assert.assertEquals(mockExample, mockProvider.getExample())
  }

This code fails with the following exception:

no answer found for: Example(#4).unbox-impl()

I think that if I were able to disable class inlining that this would no longer be an issue.

dta
  • 654
  • 4
  • 19
  • 3
    Why would you ever want to mock value classes in general? You should just be creating real instances. – Louis Wasserman Oct 07 '22 at 21:41
  • 1
    What version of MockK and Kotlin are you using? – aSemy Oct 07 '22 at 21:52
  • @LouisWasserman my initial reason for following this line of investigation is to work around the limitation against being able to mock primitives. You can test against real instances for both primitives and value classes, but using mocks makes your test apply to a more general case – dta Oct 09 '22 at 01:39
  • @aSemy kotlin 1.17.20, mockk 1.13.2, both latest versions – dta Oct 09 '22 at 01:40
  • 1
    There's no value in a test that doesn't model real behavior. There's no more reason to mock value classes than there is to mock primitives, collections, or data classes: you should never do it and always use real implementations. – Louis Wasserman Oct 09 '22 at 05:01
  • MockK's support for value classes is limited. While it's improved in MockK 1.13, it's not perfect. Can you make an issue on https://github.com/mockk/mockk? – aSemy Oct 09 '22 at 07:26
  • I've submitted an issue (with an even small reproduction case) here: https://github.com/mockk/mockk/issues/948 – dta Oct 12 '22 at 13:41

0 Answers0