Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Coroutine Mockk Example
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.mockk
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.junit.Test
class CoroutinesTest {
@Test
fun `coroutines test`() {
val arithmetic = mockk<Arithmetic>(relaxed = true)
coEvery { arithmetic.getNumber() } returns 5
GlobalScope.launch {
arithmetic.doMath()
}
coVerify { arithmetic.getNumber() }
}
class Arithmetic {
suspend fun doMath(): Int = 1 + 2 + getNumber()
suspend fun getNumber(): Int {
delay(1000)
return 3
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment