How to mock lambda with mockito in kotlin

You can do that like this:

val function: Array<Composition>) -> Unit = {}
val callback = mock(function::class.java)

getCompositons(callback)

verify(callback)(any()) // or for example verifyNoInteractions(callback)

No extra libraries besides the standard mockito are needed


This is really no different to mocking any other type:

val callback = mock<(Array<Composition>) -> Unit>()

getCompositons(callback)

verify(callback)(any())  // Or verify(callback).invoke(any()) to be explicit

(In case you weren't aware of them, I'm using the mockito-kotlin bindings here.)

Tags:

Mockito

Kotlin