Test Overview
This test demonstrates how to use @Nested inner classes in JUnit to group related test cases. It verifies that the outer and inner test methods run correctly and independently.
This test demonstrates how to use @Nested inner classes in JUnit to group related test cases. It verifies that the outer and inner test methods run correctly and independently.
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; public class CalculatorTest { @Test void testAddition() { assertEquals(5, 2 + 3); } @Nested class SubtractionTests { @Test void testSimpleSubtraction() { assertEquals(2, 5 - 3); } @Test void testNegativeResult() { assertEquals(-1, 2 - 3); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test runner starts and loads CalculatorTest class | JUnit test environment initialized | - | PASS |
| 2 | Runs outer test method testAddition() | CalculatorTest instance created | assertEquals(5, 2 + 3) verifies 5 equals 5 | PASS |
| 3 | Discovers @Nested class SubtractionTests | JUnit prepares to run nested tests | - | PASS |
| 4 | Runs nested test method testSimpleSubtraction() | SubtractionTests instance created | assertEquals(2, 5 - 3) verifies 2 equals 2 | PASS |
| 5 | Runs nested test method testNegativeResult() | SubtractionTests instance created | assertEquals(-1, 2 - 3) verifies -1 equals -1 | PASS |
| 6 | Test runner completes all tests | All tests executed successfully | - | PASS |