Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start a coroutine test using runTest.
Kotlin
import kotlinx.coroutines.test.runTest fun main() = runTest([1]) { // test code here }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces directly after runTest without parentheses causes a syntax error.
✗ Incorrect
The runTest function takes a lambda as a parameter, so it requires parentheses before the lambda block.
2fill in blank
mediumComplete the code to launch a coroutine inside runTest.
Kotlin
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.launch fun main() = runTest { val job = [1] { // coroutine work } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using async instead of launch when no result is needed.
Using runBlocking inside runTest causes blocking issues.
✗ Incorrect
Inside runTest, you use launch to start a new coroutine job.
3fill in blank
hardFix the error in the test code by completing the runTest call correctly.
Kotlin
import kotlinx.coroutines.test.runTest fun testSomething() = runTest([1]) { // test logic }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces directly after runTest without parentheses.
✗ Incorrect
runTest requires parentheses before the lambda block to be called correctly.
4fill in blank
hardComplete the code to correctly create a test coroutine that delays and asserts a value.
Kotlin
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.delay import kotlin.test.assertEquals fun main() = runTest() { delay(100) val result = 5 assertEquals([1], result) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces instead of parentheses for runTest.
Using wrong expected value in assertEquals.
✗ Incorrect
runTest needs parentheses before the lambda, and assertEquals compares expected value 5 to result.
5fill in blank
hardFill both blanks to create a test that launches a coroutine, delays, and cancels the job.
Kotlin
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.launch import kotlinx.coroutines.delay fun main() = runTest() { val job = [1] { delay(1000) } [2].cancel() }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces instead of parentheses for runTest.
Using wrong variable name instead of job to cancel.
✗ Incorrect
runTest needs parentheses before the lambda, launch starts the coroutine, and job.cancel() cancels it.