Created
December 6, 2018 18:54
-
-
Save michaelbukachi/986c409684997b84a4c25cb81f9ec524 to your computer and use it in GitHub Desktop.
Coroutine Mockk Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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