Skip to content

Instantly share code, notes, and snippets.

@michaelbukachi
Created December 6, 2018 18:54
Show Gist options
  • Save michaelbukachi/986c409684997b84a4c25cb81f9ec524 to your computer and use it in GitHub Desktop.
Save michaelbukachi/986c409684997b84a4c25cb81f9ec524 to your computer and use it in GitHub Desktop.
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