Test Overview
This test demonstrates how to use @DisplayName in JUnit to give a readable name to a test method. It verifies that the addition of two numbers returns the correct result.
This test demonstrates how to use @DisplayName in JUnit to give a readable name to a test method. It verifies that the addition of two numbers returns the correct result.
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; public class CalculatorTest { @Test @DisplayName("Adding two positive numbers should return their sum") void testAddition() { int a = 5; int b = 7; int result = a + b; assertEquals(12, result, "Sum should be 12"); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test runner starts and identifies test methods with @Test annotation | JUnit test environment initialized, CalculatorTest class loaded | - | PASS |
| 2 | Test runner reads @DisplayName annotation for testAddition method | Test method labeled as 'Adding two positive numbers should return their sum' in test report | - | PASS |
| 3 | Test runner executes testAddition method | Variables a=5, b=7 initialized; addition performed | assertEquals(12, result) verifies result is 12 | PASS |
| 4 | Test runner reports test result with readable name from @DisplayName | Test report shows 'Adding two positive numbers should return their sum' as test name with status PASS | - | PASS |