Challenge - 5 Problems
Coroutine Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this coroutine test using runTest?
Consider this Kotlin coroutine test using runTest. What will be printed when the test runs?
Kotlin
import kotlinx.coroutines.* import kotlinx.coroutines.test.runTest fun fetchData(): String = "Data" @OptIn(ExperimentalCoroutinesApi::class) suspend fun fetchDataAsync(): String = coroutineScope { delay(100) fetchData() } @OptIn(ExperimentalCoroutinesApi::class) suspend fun main() = runTest { val result = fetchDataAsync() println(result) }
Attempts:
2 left
💡 Hint
runTest executes coroutines in a controlled environment and waits for completion.
✗ Incorrect
The runTest function runs the coroutine and waits for all delays to complete. The fetchDataAsync suspends for 100ms then returns "Data", which is printed.
🧠 Conceptual
intermediate1:30remaining
Which statement about runTest is true?
Choose the correct statement about the runTest function in Kotlin coroutine testing.
Attempts:
2 left
💡 Hint
Think about how runTest handles delays without slowing down tests.
✗ Incorrect
runTest uses a TestCoroutineScheduler that advances virtual time automatically, so delays inside coroutines don't slow down tests.
🔧 Debug
advanced2:00remaining
What error does this coroutine test raise?
This test uses runTest but throws an error. What is the error type?
Kotlin
import kotlinx.coroutines.* import kotlinx.coroutines.test.runTest @OptIn(ExperimentalCoroutinesApi::class) suspend fun faultyFunction() { withContext(Dispatchers.IO) { delay(50) } } @OptIn(ExperimentalCoroutinesApi::class) suspend fun main() = runTest { faultyFunction() }
Attempts:
2 left
💡 Hint
runTest restricts usage of certain dispatchers to keep tests deterministic.
✗ Incorrect
runTest uses a TestDispatcher and disallows switching to Dispatchers.IO or Default inside its scope, causing IllegalStateException.
📝 Syntax
advanced1:30remaining
Which option correctly uses runTest to test a suspend function?
Select the code snippet that correctly tests a suspend function using runTest.
Attempts:
2 left
💡 Hint
runTest waits for all coroutines inside its block to complete before returning.
✗ Incorrect
Option D correctly calls the suspend function inside runTest and asserts the result synchronously. Option D launches a child coroutine but does not wait for it explicitly, which can cause flaky tests.
🚀 Application
expert2:30remaining
How many items are in the list after this runTest block?
Given this Kotlin coroutine test using runTest, how many items does the list contain after execution?
Kotlin
import kotlinx.coroutines.* import kotlinx.coroutines.test.runTest @OptIn(ExperimentalCoroutinesApi::class) suspend fun addItems(list: MutableList<Int>) { coroutineScope { launch { delay(50) list.add(1) } launch { delay(100) list.add(2) } } } @OptIn(ExperimentalCoroutinesApi::class) suspend fun main() = runTest { val items = mutableListOf<Int>() addItems(items) println(items.size) }
Attempts:
2 left
💡 Hint
runTest waits for all child coroutines launched inside coroutineScope to finish.
✗ Incorrect
Both launched coroutines complete before runTest returns, so both items are added to the list. The size is 2.