Challenge - 5 Problems
JUnit First Test Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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); } }
Attempts:
2 left
💡 Hint
Check the assertion and the calculation inside the test method.
✗ Incorrect
The test method calculates 2 + 3 which equals 5, and asserts that the result equals 5. Since this is true, the test passes without errors.
❓ assertion
intermediate1:30remaining
Choosing the correct assertion for null check
Which assertion correctly verifies that an object reference is null in a JUnit test?
Attempts:
2 left
💡 Hint
JUnit provides a specific assertion method for null checks.
✗ Incorrect
assertNull(myObject) is the dedicated JUnit assertion to check if myObject is null. While B and C might work, A is the best practice and most readable.
🔧 Debug
advanced2: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; } }
Attempts:
2 left
💡 Hint
Think about what happens when dividing by zero in Java.
✗ Incorrect
Dividing by zero in Java causes an ArithmeticException at runtime, so the test fails with an exception before the assertion.
❓ framework
advanced1:30remaining
JUnit 5 annotation for setup before each test
Which annotation runs a method before each test method in a JUnit 5 test class?
Attempts:
2 left
💡 Hint
This annotation runs before every test method, not just once.
✗ Incorrect
@BeforeEach runs the annotated method before each test method. @BeforeAll runs once before all tests.
🧠 Conceptual
expert3: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?
Attempts:
2 left
💡 Hint
Think about what happens if tests share data or state.
✗ Incorrect
Running each test in a fresh instance prevents tests from interfering with each other, making tests reliable and easier to debug.