0
0
Kotlinprogramming~20 mins

Testing coroutines with runTest in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Coroutine Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
}
AData
Bnull
CException in thread "main" java.lang.IllegalStateException
DNo output (test hangs)
Attempts:
2 left
💡 Hint
runTest executes coroutines in a controlled environment and waits for completion.
🧠 Conceptual
intermediate
1:30remaining
Which statement about runTest is true?
Choose the correct statement about the runTest function in Kotlin coroutine testing.
ArunTest disables all coroutine dispatchers during the test.
BrunTest launches coroutines but does not wait for their completion.
CrunTest automatically advances virtual time to handle delays inside coroutines.
DrunTest can only be used with blocking code, not suspend functions.
Attempts:
2 left
💡 Hint
Think about how runTest handles delays without slowing down tests.
🔧 Debug
advanced
2: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()
}
AIllegalStateException: Dispatchers.IO is not allowed inside runTest
BTimeoutCancellationException
CNo error, runs successfully
DUnresolved reference: Dispatchers
Attempts:
2 left
💡 Hint
runTest restricts usage of certain dispatchers to keep tests deterministic.
📝 Syntax
advanced
1:30remaining
Which option correctly uses runTest to test a suspend function?
Select the code snippet that correctly tests a suspend function using runTest.
A
runTest {
    launch {
        val result = suspendFunction()
        assert(result == 42)
    }
}
B
runTest {
    val result = suspendFunction()
    println(result)
}
Thread.sleep(100)
C
runTest {
    suspendFunction()
}
println("Test done")
D
runTest {
    val result = suspendFunction()
    assert(result == 42)
}
Attempts:
2 left
💡 Hint
runTest waits for all coroutines inside its block to complete before returning.
🚀 Application
expert
2: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)
}
ATest hangs and prints nothing
B2
C1
D0
Attempts:
2 left
💡 Hint
runTest waits for all child coroutines launched inside coroutineScope to finish.