0
0
JUnittesting~8 mins

@DisplayName for readable names in JUnit - Framework Patterns

Choose your learning style9 modes available
Framework Mode - @DisplayName for readable names
Folder Structure
project-root/
├── src/
│   ├── main/
│   │   └── java/
│   │       └── com/example/app/
│   │           └── App.java
│   └── test/
│       └── java/
│           └── com/example/app/
│               ├── tests/
│               │   └── LoginTests.java
│               ├── pages/
│               │   └── LoginPage.java
│               └── utils/
│                   └── TestUtils.java
└── pom.xml
    
Test Framework Layers
  • Test Classes: Contain test methods annotated with @Test and @DisplayName for readable test names.
  • Page Objects: Represent UI pages with methods to interact with page elements.
  • Utilities: Helper classes for common functions like waits, data setup, or assertions.
  • Configuration: Manage environment settings, browser setup, and test parameters.
  • Test Runner: JUnit platform runs tests and uses @DisplayName to show readable names in reports.
Configuration Patterns
  • Use src/test/resources for config files like application.properties or test.properties.
  • Use Maven profiles or system properties to switch environments (dev, test, prod).
  • Configure browser and other settings in a central config class or via dependency injection.
  • Keep credentials secure and load them from environment variables or encrypted files.
  • JUnit 5 supports @BeforeAll and @BeforeEach to set up test context.
Test Reporting and CI/CD Integration
  • JUnit 5 generates XML reports compatible with CI tools like Jenkins, GitHub Actions, or GitLab.
  • @DisplayName annotations improve report readability by showing descriptive test names instead of method names.
  • Integrate with build tools (Maven/Gradle) to run tests automatically on code commits.
  • Use plugins like Surefire or Failsafe to generate detailed test reports.
  • CI pipelines can fail builds on test failures and notify teams with clear test names from @DisplayName.
Best Practices
  1. Use @DisplayName to write clear, human-readable test names that describe the test purpose.
  2. Keep test method names concise but use @DisplayName for detailed explanations.
  3. Consistently apply @DisplayName across all test methods for uniform reports.
  4. Use natural language in @DisplayName to help non-technical stakeholders understand test results.
  5. Combine @DisplayName with nested tests or parameterized tests for better structure and clarity.
Self Check

Where in this folder structure would you add a new test method with a @DisplayName annotation for a feature called "User Registration"?

Key Result
Use @DisplayName in JUnit tests to provide clear, readable test names for better reports and understanding.