mockito / mockito-kotlin

Using Mockito with Kotlin
MIT License
3.11k stars 202 forks source link

Mocking functions with reified #324

Closed jshvarts closed 5 years ago

jshvarts commented 5 years ago

Hi Niek,

Thanks again for the great library.

I am wondering if mocking functions with reified is supported in mockito-kotin.

I have this function in class MyClass:

inline fun <reified T : SomeType> isSettingsEnabled(type: OtherType): Boolean = when (T::class) {
   // do something
}

And I try to mock it with:

  val myClass = mock() 
  whenever(myClass.isSettingsEnabled<ContentType.Collection>(OtherType.ONE))
   .thenReturn(true)

Which results in:

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.

Mocking another function that does not use reified on the same class works fine.

I am using mockito-kotlin:2.1.0

Thanks!

jshvarts commented 5 years ago

Closing the ticket. I realized now that all inline (not just reified) functions are not mockable.

aouledissa commented 4 years ago

@jshvarts what about new mockito release? Ideally all inline function are translated as final functions in java notation. I am not sure how they become in bytecode but maybe this can help you https://www.baeldung.com/mockito-final