Bird
Raised Fist0
Android Kotlinmobile~20 mins

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

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

Practice

(1/5)
1. What is the main purpose of a unit test in Android Kotlin development using JUnit?
easy
A. To deploy the app to the Play Store
B. To test the entire application UI
C. To check if a small part of code works correctly
D. To measure app performance under load

Solution

  1. Step 1: Understand unit test scope

    Unit tests focus on small, isolated parts of code, like functions or classes.
  2. Step 2: Differentiate from other tests

    UI tests and performance tests cover broader areas, not unit tests.
  3. Final Answer:

    To check if a small part of code works correctly -> Option C
  4. Quick Check:

    Unit test = small code check [OK]
Hint: Unit tests check small code parts only [OK]
Common Mistakes:
  • Confusing unit tests with UI or integration tests
  • Thinking unit tests deploy or measure performance
  • Assuming unit tests cover the whole app
2. Which annotation is used in Kotlin with JUnit to mark a function as a test?
easy
A. @Test
B. @Before
C. @RunWith
D. @Ignore

Solution

  1. Step 1: Identify JUnit test annotation

    The @Test annotation marks a function as a test method in JUnit.
  2. Step 2: Understand other annotations

    @Before runs setup code, @RunWith specifies runner, @Ignore skips tests.
  3. Final Answer:

    @Test -> Option A
  4. Quick Check:

    @Test marks test functions [OK]
Hint: Use @Test to mark test functions [OK]
Common Mistakes:
  • Using @Before instead of @Test for tests
  • Confusing @RunWith with test marking
  • Mistaking @Ignore as test annotation
3. Given this Kotlin JUnit test code, what will be the test result?
import org.junit.Test
import kotlin.test.assertEquals

class CalculatorTest {
    @Test
    fun testAdd() {
        val result = 2 + 3
        assertEquals(5, result)
    }
}
medium
A. Test passes successfully
B. Test fails with assertion error
C. Compilation error due to missing import
D. Runtime exception occurs

Solution

  1. Step 1: Analyze the test logic

    The test adds 2 + 3 and stores in result, which equals 5.
  2. Step 2: Check assertion correctness

    assertEquals expects 5 and result is 5, so assertion passes.
  3. Final Answer:

    Test passes successfully -> Option A
  4. Quick Check:

    2 + 3 = 5 matches assertEquals [OK]
Hint: Check if expected equals actual in assertEquals [OK]
Common Mistakes:
  • Assuming test fails when values match
  • Thinking imports are missing when they are correct
  • Confusing runtime errors with assertion failures
4. Identify the error in this Kotlin JUnit test code:
import org.junit.Test
import kotlin.test.assertEquals

class CalculatorTest {
    @Test
    fun testSubtract() {
        val result = 5 - 2
        assertEquals(4, result)
    }
}
medium
A. Function name should start with 'test_'
B. Missing @Test annotation
C. Incorrect import statement
D. Wrong expected value in assertEquals

Solution

  1. Step 1: Calculate the actual result

    5 - 2 equals 3, but expected value is 4 in assertEquals.
  2. Step 2: Check annotations and imports

    @Test annotation and imports are correct; function name style is not enforced.
  3. Final Answer:

    Wrong expected value in assertEquals -> Option D
  4. Quick Check:

    5 - 2 = 3, expected 4 causes failure [OK]
Hint: Verify expected matches actual in assertions [OK]
Common Mistakes:
  • Ignoring mismatch between expected and actual values
  • Thinking function names must start with 'test_'
  • Assuming missing annotations when present
5. You want to test a Kotlin function that returns null for invalid input. Which assertion correctly checks this behavior in JUnit?
hard
A. assertEquals(null, functionResult)
B. assertNull(functionResult)
C. assertTrue(functionResult == null)
D. assertNotNull(functionResult)

Solution

  1. Step 1: Understand null checking assertions

    assertNull explicitly checks if a value is null, making tests clear and readable.
  2. Step 2: Compare other options

    assertEquals(null, ...) works but is less clear; assertTrue(...) works but less direct; assertNotNull is opposite.
  3. Final Answer:

    assertNull(functionResult) -> Option B
  4. Quick Check:

    Use assertNull to check null values [OK]
Hint: Use assertNull to check if value is null [OK]
Common Mistakes:
  • Using assertNotNull when expecting null
  • Using assertEquals(null, ...) instead of assertNull
  • Using assertTrue with complex conditions unnecessarily
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