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 |
@DisplayName in JUnit tests?@DisplayName in a JUnit 5 test method?@Test
@DisplayName("Check if number is positive")
void isPositiveTest() {
assertTrue(5 > 0);
}@DisplayName:@Test
@DisplayName(Test addition)
void testAdd() {
assertEquals(4, 2 + 2);
}Test "add" method works. Which is the correct way to write the @DisplayName annotation?Pass the test and this pattern moves unknown