In kotlin, how do I mock a suspend function that wraps a callback?
I suggest using MockK for your tests, which is more coroutine-friendly.
To mock a coroutine, you can use coEvery
and returns
like below:
val interf = mockk<SomeInterface>()
coEvery { a.doSomething(any()) } returns Outcome.OK
When you need to
on .... do return ..
and method is suspended, with mockito you can use this:
- use this lib mockito-kotlin
- Mock your object, lets name it myObject (myObject has suspend method named isFoo)
then:
myObject.stub {
onBlocking { isFoo() }.doReturn(true)
}