Challenge - 5 Problems
JUnit Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)) }
Attempts:
2 left
💡 Hint
Check if the expected value matches the actual multiplication result.
✗ Incorrect
The multiply function returns 4 * 5 = 20, which matches the expected value in assertEquals, so the test passes.
❓ assertion
intermediate2: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")
Attempts:
2 left
💡 Hint
Check which assertion directly compares the size to 3.
✗ Incorrect
assertEquals(3, items.size) directly checks that the list size is exactly 3. Other options either check different conditions or cause errors.
🔧 Debug
advanced2: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!!) }
Attempts:
2 left
💡 Hint
Check what happens when divide returns null and !! is used.
✗ Incorrect
The divide function returns null when dividing by zero. Using result!! forces a non-null assertion on null, causing NullPointerException.
🧠 Conceptual
advanced2: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?
Attempts:
2 left
💡 Hint
Think about setup tasks needed before every test.
✗ Incorrect
@BeforeEach runs the annotated method before every test method to set up test conditions.
❓ framework
expert2: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?
Attempts:
2 left
💡 Hint
Look for annotations that provide input data to tests.
✗ Incorrect
@ParameterizedTest combined with @ValueSource or @CsvSource runs the test multiple times with different inputs.