0
0
Kotlinprogramming~20 mins

Kotlin test assertions - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kotlin Test Assertions 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 Kotlin test assertion code?
Consider the following Kotlin test code using JUnit assertions. What will be the output when this test runs?
Kotlin
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test

class SampleTest {
    @Test
    fun testSum() {
        val result = 2 + 3
        assertEquals(5, result)
        assertTrue(result > 4)
        assertFalse(result < 0)
    }
}
ATest passes with no errors
BTest fails with AssertionError on assertEquals
CTest fails with AssertionError on assertTrue
DTest fails with AssertionError on assertFalse
Attempts:
2 left
💡 Hint
Check the values compared in the assertions carefully.
Predict Output
intermediate
2:00remaining
What error does this Kotlin test assertion code raise?
What error will be raised when running this Kotlin test code?
Kotlin
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test

class SampleTest {
    @Test
    fun testString() {
        val actual = "Kotlin"
        assertEquals("kotlin", actual)
    }
}
ANo error, test passes
BNullPointerException
CAssertionError due to case mismatch
DCompilation error
Attempts:
2 left
💡 Hint
assertEquals is case sensitive for strings.
🔧 Debug
advanced
2:00remaining
Why does this Kotlin test assertion fail unexpectedly?
This Kotlin test code fails with an AssertionError. Identify the reason.
Kotlin
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test

class SampleTest {
    @Test
    fun testList() {
        val list1 = listOf(1, 2, 3)
        val list2 = listOf(3, 2, 1)
        assertEquals(list1, list2)
    }
}
ALists have same elements but different order
BLists have different sizes
CassertEquals cannot compare lists
DCompilation error due to missing import
Attempts:
2 left
💡 Hint
assertEquals compares lists element by element in order.
📝 Syntax
advanced
2:00remaining
Which option correctly asserts that a nullable variable is null in Kotlin test?
Given a nullable variable val name: String? = null, which assertion correctly checks it is null?
AassertNull(name)
BassertEquals(null, name)
CassertTrue(name == null)
DAll of the above
Attempts:
2 left
💡 Hint
Check which assertions are valid and equivalent for null checks.
🚀 Application
expert
2:00remaining
What is the output of this Kotlin test assertion with exception checking?
What will happen when running this Kotlin test code that checks for an exception?
Kotlin
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test

class SampleTest {
    @Test
    fun testException() {
        val exception = assertThrows<IllegalArgumentException> {
            throw IllegalArgumentException("Invalid argument")
        }
        assertEquals("Invalid argument", exception.message)
    }
}
ATest fails due to message mismatch
BTest passes with no errors
CTest fails due to wrong exception type
DTest fails because no exception is thrown
Attempts:
2 left
💡 Hint
assertThrows expects the specified exception type to be thrown inside the lambda.