Search code examples
kotlinmockitomockito-kotlin

How to specify Mockito mock extraInterfaces in Kotlin (using nhaarman mockitokotlin2)


I understand how to specify extraInterface using the @Mock annotation, but how can I create a mock and add the extraInterfaces inline?

@SmallTest
@RunWith(MockitoJUnitRunner::class)
class MyTestClass {
    
    @Mock(extraInterfaces = [MyCallback::class])
    lateinit var callbackFragment: Fragment
    ...
}

But how can I do this on the fly?

// this doesn't compile
val callbackFragment = mock<Fragment>(extraInterfaces = [MyCallback::class])

What is the correct syntax for adding extraInterfaces to a Mockito mock in Kotlin?


Solution

  • This should work:

     val mock = mock<Fragment>(extraInterfaces = arrayOf(MyCallback::class))