Challenge - 5 Problems
Kotlin Test Assertions 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 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) } }
Attempts:
2 left
💡 Hint
Check the values compared in the assertions carefully.
✗ Incorrect
The sum 2 + 3 equals 5, so assertEquals(5, result) passes. result > 4 is true, so assertTrue passes. result < 0 is false, so assertFalse passes. All assertions succeed, so the test passes.
❓ Predict Output
intermediate2: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) } }
Attempts:
2 left
💡 Hint
assertEquals is case sensitive for strings.
✗ Incorrect
The expected string "kotlin" is lowercase, but actual is "Kotlin" with uppercase K. assertEquals fails and throws AssertionError.
🔧 Debug
advanced2: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) } }
Attempts:
2 left
💡 Hint
assertEquals compares lists element by element in order.
✗ Incorrect
Although both lists contain the same numbers, their order differs. assertEquals compares elements in order, so the test fails.
📝 Syntax
advanced2: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?
Attempts:
2 left
💡 Hint
Check which assertions are valid and equivalent for null checks.
✗ Incorrect
assertNull(name) explicitly checks for null. assertEquals(null, name) compares to null. assertTrue(name == null) checks condition. All are valid ways to assert null.
🚀 Application
expert2: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) } }
Attempts:
2 left
💡 Hint
assertThrows expects the specified exception type to be thrown inside the lambda.
✗ Incorrect
The code throws IllegalArgumentException with message "Invalid argument". assertThrows catches it and returns the exception. The message matches, so all assertions pass.