0
0
JUnittesting~10 mins

@DisplayName for readable names in JUnit - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - JUnit
JUnit
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");
    }
}
Execution Trace - 4 Steps
StepActionSystem StateAssertionResult
1Test runner starts and identifies test methods with @Test annotationJUnit test environment initialized, CalculatorTest class loaded-PASS
2Test runner reads @DisplayName annotation for testAddition methodTest method labeled as 'Adding two positive numbers should return their sum' in test report-PASS
3Test runner executes testAddition methodVariables a=5, b=7 initialized; addition performedassertEquals(12, result) verifies result is 12PASS
4Test runner reports test result with readable name from @DisplayNameTest report shows 'Adding two positive numbers should return their sum' as test name with status PASS-PASS
Failure Scenario
Failing Condition: The addition result is incorrect (e.g., result is not 12)
Execution Trace Quiz - 3 Questions
Test your understanding
What does the @DisplayName annotation do in this test?
ASkips the test during execution
BProvides a readable name for the test method in reports
CChanges the method name in the code
DRuns the test multiple times
Key Result
Using @DisplayName helps make test reports clearer and easier to understand by giving tests descriptive names instead of method names.