0
0
JUnittesting~20 mins

Unit testing concept in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
JUnit Unit Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
JUnit test method output
What will be the result of running this JUnit 5 test method?
JUnit
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

class CalculatorTest {
    @Test
    void testAdd() {
        int result = 2 + 3;
        assertEquals(5, result);
    }
}
ATest fails with AssertionError
BRuntime NullPointerException
CCompilation error due to missing import
DTest passes successfully
Attempts:
2 left
πŸ’‘ Hint
Check the assertion and the calculation result.
❓ assertion
intermediate
2:00remaining
Correct assertion for exception testing
Which JUnit 5 assertion correctly tests that a method throws a NullPointerException?
JUnit
void methodThatThrows() {
    throw new NullPointerException();
}
AassertThrows(NullPointerException.class, () -> methodThatThrows());
BassertEquals(NullPointerException.class, methodThatThrows());
CassertTrue(methodThatThrows() instanceof NullPointerException);
DassertNull(methodThatThrows());
Attempts:
2 left
πŸ’‘ Hint
Look for the assertion designed to check exceptions.
πŸ”§ Debug
advanced
2:00remaining
Identify the cause of test failure
Given this JUnit 5 test, why does it fail?
JUnit
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

class StringTest {
    @Test
    void testLength() {
        String s = null;
        assertEquals(4, s.length());
    }
}
ATest passes because null length is treated as 0
BAssertionError because length is not 4
CNullPointerException because s is null when calling length()
DCompilation error due to missing import
Attempts:
2 left
πŸ’‘ Hint
Think about calling a method on a null object.
🧠 Conceptual
advanced
1:30remaining
Purpose of unit testing
What is the main purpose of unit testing in software development?
ATo check the performance of the system under load
BTo verify that individual parts of code work correctly in isolation
CTo deploy the software to production automatically
DTo test the entire application’s user interface
Attempts:
2 left
πŸ’‘ Hint
Unit testing focuses on small pieces of code, not the whole system.
❓ framework
expert
2:30remaining
JUnit 5 lifecycle method order
What is the correct order of JUnit 5 lifecycle methods when running multiple tests in a class?
A1, 2, 3, 4, 5
B1, 3, 2, 4, 5
C2, 1, 3, 4, 5
D2, 3, 4, 1, 5
Attempts:
2 left
πŸ’‘ Hint
Remember which methods run once per class and which run before/after each test.