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

Cannot mock boxed (primitive?) types #533

Open
wegendt-bosch opened this issue Feb 3, 2025 · 0 comments
Open

Cannot mock boxed (primitive?) types #533

wegendt-bosch opened this issue Feb 3, 2025 · 0 comments

Comments

@wegendt-bosch
Copy link

wegendt-bosch commented Feb 3, 2025

Previously worked in version 5.1.0.

This test fails:

@ExtendWith(MockitoExtension::class)
class MockitoTest {
  class Consumer<T>(val fn: (T) -> Unit)
  class Mocked {
    fun <T> callConsumer(consumer: Consumer<T>, value: T) {
      consumer.fn(value)
    }
  }

  @Mock
  private lateinit var mocked: Mocked

  @Test
  fun testPrimitives() {
    var functionCalled = false
    val aConsumer = Consumer<UShort> {}
    whenever(mocked.callConsumer(same(aConsumer), any())).then { functionCalled = true }

    mocked.callConsumer(aConsumer, 42u)
    assertTrue(functionCalled)
  }
}

but works when you change the type parameter to Short:

@ExtendWith(MockitoExtension::class)
class MockitoTest {
  class Consumer<T>(val fn: (T) -> Unit)
  class Mocked {
    fun <T> callConsumer(consumer: Consumer<T>, value: T) {
      consumer.fn(value)
    }
  }

  @Mock
  private lateinit var mocked: Mocked

  @Test
  fun testPrimitives() {
    var functionCalled = false
    val aConsumer = Consumer<Short> {}
    whenever(mocked.callConsumer(same(aConsumer), any())).then { functionCalled = true }

    mocked.callConsumer(aConsumer, 42)
    assertTrue(functionCalled)
  }
}
@wegendt-bosch wegendt-bosch changed the title Cannot mock boxed types Cannot mock boxed (primitive?) types Feb 3, 2025
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