Test Overview
This test demonstrates how to name JUnit test methods clearly to describe what they verify. It checks that a simple calculator adds numbers correctly and that the test name reflects the behavior being tested.
This test demonstrates how to name JUnit test methods clearly to describe what they verify. It checks that a simple calculator adds numbers correctly and that the test name reflects the behavior being tested.
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class CalculatorTest { @Test void add_TwoPositiveNumbers_ReturnsCorrectSum() { Calculator calc = new Calculator(); int result = calc.add(3, 5); assertEquals(8, result, "Sum should be 8"); } } class Calculator { public int add(int a, int b) { return a + b; } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test runner starts and loads CalculatorTest class | JUnit test environment initialized | - | PASS |
| 2 | JUnit finds method add_TwoPositiveNumbers_ReturnsCorrectSum annotated with @Test | Test method ready to execute | - | PASS |
| 3 | Test method creates Calculator instance | Calculator object instantiated | - | PASS |
| 4 | Test method calls add(3, 5) on Calculator | Calculator computes sum 8 | - | PASS |
| 5 | Test method asserts result equals 8 | Assertion compares expected 8 with actual 8 | assertEquals(8, result) | PASS |
| 6 | Test method completes successfully | Test marked as passed in report | - | PASS |