Bird
Raised Fist0
Kotlinprogramming~20 mins

Kotlin test assertions - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. Which Kotlin test assertion checks if two values are equal?
easy
A. assertEquals(expected, actual)
B. assertTrue(condition)
C. assertNull(value)
D. assertFails { block }

Solution

  1. Step 1: Understand assertion types

    assertEquals is used to check if two values are equal.
  2. Step 2: Match assertion to purpose

    assertTrue checks if a condition is true, assertNull checks for null, and assertFails checks for exceptions.
  3. Final Answer:

    assertEquals(expected, actual) -> Option A
  4. Quick Check:

    Equality check = assertEquals [OK]
Hint: Use assertEquals to compare expected and actual values [OK]
Common Mistakes:
  • Confusing assertTrue with assertEquals
  • Using assertNull for equality checks
  • Using assertFails to check values
2. Which of the following is the correct syntax to assert a condition is true in Kotlin tests?
easy
A. assertTrue { condition }
B. assertTrue condition
C. assertTrue(condition)
D. assertTrue: condition

Solution

  1. Step 1: Recall Kotlin assertion syntax

    assertTrue takes a Boolean condition as a parameter inside parentheses.
  2. Step 2: Identify correct syntax

    Only assertTrue(condition) is valid Kotlin syntax; others are incorrect.
  3. Final Answer:

    assertTrue(condition) -> Option C
  4. Quick Check:

    assertTrue uses parentheses for condition [OK]
Hint: Remember assertions use parentheses for arguments [OK]
Common Mistakes:
  • Using curly braces instead of parentheses
  • Omitting parentheses
  • Using colon instead of parentheses
3. What will be the result of this Kotlin test assertion?
val result = 5
assertEquals(5, result)
medium
A. Test passes without error
B. Test fails with AssertionError
C. Compilation error
D. Runtime exception other than AssertionError

Solution

  1. Step 1: Check values compared

    The expected value is 5 and actual value stored in result is also 5.
  2. Step 2: Understand assertEquals behavior

    Since both values are equal, assertEquals passes without throwing an error.
  3. Final Answer:

    Test passes without error -> Option A
  4. Quick Check:

    Equal values = test passes [OK]
Hint: Equal expected and actual means test passes [OK]
Common Mistakes:
  • Assuming assertEquals throws error on equal values
  • Confusing expected and actual order
  • Thinking assertEquals returns a value
4. Identify the error in this Kotlin test assertion code:
assertEquals(10, 5 + 3)
medium
A. Runtime exception due to invalid operation
B. No error, test passes
C. Syntax error due to missing parentheses
D. Test fails because expected and actual differ

Solution

  1. Step 1: Calculate actual value

    The expression 5 + 3 equals 8.
  2. Step 2: Compare expected and actual

    Expected is 10 but actual is 8, so assertEquals fails and throws AssertionError.
  3. Final Answer:

    Test fails because expected and actual differ -> Option D
  4. Quick Check:

    Different values = test fails [OK]
Hint: Check actual calculation matches expected value [OK]
Common Mistakes:
  • Assuming test passes despite value mismatch
  • Thinking syntax error occurs
  • Confusing runtime exceptions with assertion failures
5. You want to test that a function throws an exception when given invalid input. Which Kotlin assertion should you use?
hard
A. assertNull(function(invalidInput))
B. assertFails { function(invalidInput) }
C. assertEquals(exception, function(invalidInput))
D. assertTrue(function(invalidInput))

Solution

  1. Step 1: Understand exception testing

    To check if a function throws an exception, use assertFails with a lambda calling the function.
  2. Step 2: Evaluate other options

    assertEquals compares values, not exceptions; assertNull checks for null; assertTrue checks a Boolean condition.
  3. Final Answer:

    assertFails { function(invalidInput) } -> Option B
  4. Quick Check:

    Exception test = assertFails [OK]
Hint: Use assertFails with lambda to test exceptions [OK]
Common Mistakes:
  • Using assertEquals to check exceptions
  • Using assertNull expecting exception
  • Using assertTrue for exception testing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard