iadcialim24
iadcialim24

Reputation: 4035

Mockito: Mock an invokable param

Kotlin function to test:

suspend fun <T: Any> handleSomething(call: suspend () -> Result<T>) {

    if (call.invoke() == "Something") {
        ...
    }
    else {
        ...
    }
}

I want to mock call here. Usually I mock like:

val call = Mockito.mock(SomeClass::class.java)

But I don't know for param as function like this

Upvotes: 0

Views: 183

Answers (1)

Animesh Sahu
Animesh Sahu

Reputation: 8106

Original answer: https://stackoverflow.com/a/53306974/11377112

This is how you mock a lambda function:

val call = Mockito.mock<suspend () -> Result<T>>()

handleSomething(call)

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

Upvotes: 1

Related Questions