Mockito: How to mock lambda callback? what is the type? (Kotlin)

Viewed 438

I have two class TestTarget, MockTarget and test code below. If the TestTarget has two functions with same name and same count of parameter, the any() is ambiguous. I need to assign the type to any(ClassType). But what is the type of () -> Unit? I have tried Function0 and it doesn't work. Can anyone help?

Class TestTarget:

Class TestTarget(private val mockTarget: MockTarget) {
     fun testFunction() {
          // some logic to be tested.
          // call mockTarget.doSomething.
     }
}

Class MockTarget

Class MockTarget {
    fun doSomething(callback: () -> Unit) {
        // some logic here.
    }

    fun doSomething(listener: OtherType) {
        // Test code works without this function.
    }
}

Test Code:

// setup mocks.

@Test
fun `verify testFunction`() {
    `when`(mockTarget.doSomething(any())).thenAnswer { invocation ->
        // callback here.
    }
}
1 Answers

Replace org.mockito.Mockito with org.mockito.kotlin.* and you can code like this

val anyLambda = any<() -> Unit>()
val anyListener = any<OtherType>()
Related