Test Overview
This test checks a simple calculator addition method using both JUnit 4 and JUnit 5 styles. It verifies that the addition result is correct and shows how assertions and annotations differ between the two versions.
This test checks a simple calculator addition method using both JUnit 4 and JUnit 5 styles. It verifies that the addition result is correct and shows how assertions and annotations differ between the two versions.
import static org.junit.Assert.assertEquals; import org.junit.Test; public class CalculatorTestJUnit4 { @Test public void testAdd() { Calculator calc = new Calculator(); int result = calc.add(2, 3); assertEquals(5, result); } } // JUnit 5 version import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class CalculatorTestJUnit5 { @Test void testAdd() { Calculator calc = new Calculator(); int result = calc.add(2, 3); assertEquals(5, result); } } // Calculator class public class Calculator { public int add(int a, int b) { return a + b; } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | JUnit 4 test runner starts and loads CalculatorTestJUnit4 class | JUnit 4 environment ready, test class loaded | - | PASS |
| 2 | JUnit 4 runner finds @Test method testAdd() and invokes it | CalculatorTestJUnit4 instance created | - | PASS |
| 3 | testAdd() calls Calculator.add(2, 3) which returns 5 | Calculator instance created and method executed | - | PASS |
| 4 | JUnit 4 assertEquals(5, result) verifies the result is 5 | Assertion compares expected and actual values | assertEquals passes if expected == actual | PASS |
| 5 | JUnit 4 test completes successfully | Test marked as passed | - | PASS |
| 6 | JUnit 5 test runner starts and loads CalculatorTestJUnit5 class | JUnit 5 environment ready, test class loaded | - | PASS |
| 7 | JUnit 5 runner finds @Test method testAdd() and invokes it | CalculatorTestJUnit5 instance created | - | PASS |
| 8 | testAdd() calls Calculator.add(2, 3) which returns 5 | Calculator instance created and method executed | - | PASS |
| 9 | JUnit 5 assertEquals(5, result) verifies the result is 5 | Assertion compares expected and actual values | assertEquals passes if expected == actual | PASS |
| 10 | JUnit 5 test completes successfully | Test marked as passed | - | PASS |