How to mock lambda with mockito in kotlin

前端 未结 2 1518
萌比男神i
萌比男神i 2021-01-17 18:52

I have a kotlin Android app. There is a function that loads compositions from the backend and returns them to a callback:

getCompositons(callback: (Array<         


        
2条回答
  •  天命终不由人
    2021-01-17 19:23

    This is really no different to mocking any other type:

    val callback = mock<(Array) -> 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.)

提交回复
热议问题