0
0
Android Kotlinmobile~20 mins

Unit testing with JUnit in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JUnit Mastery Badge
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 JUnit test?
Consider the following Kotlin function and its JUnit test. What will be the test result when running this test?
Android Kotlin
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.assertEquals

fun multiply(a: Int, b: Int): Int {
    return a * b
}

@Test
fun testMultiply() {
    assertEquals(20, multiply(4, 5))
}
ATest fails with AssertionError
BTest passes successfully
CTest fails with NullPointerException
DTest does not compile due to missing import
Attempts:
2 left
💡 Hint
Check if the expected value matches the actual multiplication result.
assertion
intermediate
2:00remaining
Which assertion correctly tests that a list contains exactly 3 items?
You have a Kotlin list: val items = listOf("a", "b", "c"). Which JUnit assertion correctly verifies the list size is 3?
Android Kotlin
val items = listOf("a", "b", "c")
AassertEquals(3, items.size)
BassertTrue(items.count() == 3)
CassertFalse(items.isEmpty())
DassertNull(items[3])
Attempts:
2 left
💡 Hint
Check which assertion directly compares the size to 3.
🔧 Debug
advanced
2:00remaining
Why does this JUnit test fail with NullPointerException?
Examine the Kotlin test code below. Why does it throw NullPointerException during execution?
Android Kotlin
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.assertEquals

class Calculator {
    fun divide(a: Int, b: Int): Int? {
        return if (b == 0) null else a / b
    }
}

@Test
fun testDivide() {
    val calc = Calculator()
    val result = calc.divide(10, 0)
    assertEquals(0, result!!)
}
AassertEquals expects non-null but result is nullable
Bdivide function throws ArithmeticException for division by zero
Cresult is null and using !! causes NullPointerException
DCalculator class is not instantiated properly
Attempts:
2 left
💡 Hint
Check what happens when divide returns null and !! is used.
🧠 Conceptual
advanced
2:00remaining
What is the purpose of the @BeforeEach annotation in JUnit 5?
In JUnit 5, what does the @BeforeEach annotation do in a test class?
ARuns the annotated method before each test method executes
BRuns the annotated method once before all tests in the class
CMarks a test method to be ignored during execution
DSpecifies the order in which test methods run
Attempts:
2 left
💡 Hint
Think about setup tasks needed before every test.
framework
expert
2:00remaining
Which JUnit 5 feature allows parameterized tests with multiple input values?
You want to run the same test method multiple times with different input values in JUnit 5. Which feature supports this?
A@Test with multiple assert statements
B@RepeatedTest with @Test annotation
C@TestFactory with dynamic tests
D@ParameterizedTest with @ValueSource or @CsvSource annotations
Attempts:
2 left
💡 Hint
Look for annotations that provide input data to tests.