How to mock lambda with mockito in kotlin

前端 未结 2 1463
萌比男神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:07

    You can do that like this:

    val function: Array) -> 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

提交回复
热议问题