0
0
JUnittesting~20 mins

First JUnit test - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JUnit First Test Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple JUnit test
What is the result of running this JUnit test class?
JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class SimpleTest {
    @Test
    void testSum() {
        int result = 2 + 3;
        assertEquals(5, result);
    }
}
ACompilation error due to missing import.
BTest fails with an AssertionError.
CTest passes successfully with no errors.
DRuntime exception due to null pointer.
Attempts:
2 left
💡 Hint
Check the assertion and the calculation inside the test method.
assertion
intermediate
1:30remaining
Choosing the correct assertion for null check
Which assertion correctly verifies that an object reference is null in a JUnit test?
AassertNull(myObject);
BassertEquals(null, myObject);
CassertTrue(myObject == null);
DassertNotNull(myObject);
Attempts:
2 left
💡 Hint
JUnit provides a specific assertion method for null checks.
🔧 Debug
advanced
2:30remaining
Identify the cause of test failure
Why does this JUnit test fail when run?
JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class DebugTest {
    @Test
    void testDivide() {
        int result = divide(10, 0);
        assertEquals(0, result);
    }

    int divide(int a, int b) {
        return a / b;
    }
}
AThe test throws an ArithmeticException due to division by zero.
BThe test fails because the assertion expects 0 but gets 10.
CThe test fails because divide method returns null.
DThe test passes because 10 divided by 0 is 0.
Attempts:
2 left
💡 Hint
Think about what happens when dividing by zero in Java.
framework
advanced
1:30remaining
JUnit 5 annotation for setup before each test
Which annotation runs a method before each test method in a JUnit 5 test class?
A@AfterAll
B@BeforeAll
C@AfterEach
D@BeforeEach
Attempts:
2 left
💡 Hint
This annotation runs before every test method, not just once.
🧠 Conceptual
expert
3:00remaining
Understanding test isolation in JUnit
In JUnit, why is it important that each test method runs in isolation with a fresh instance of the test class?
ATo speed up test execution by reusing the same instance for all tests.
BTo prevent side effects from one test affecting another, ensuring reliable and independent tests.
CTo allow tests to share state easily between methods.
DTo reduce memory usage by creating fewer objects.
Attempts:
2 left
💡 Hint
Think about what happens if tests share data or state.