0
0
JUnittesting~8 mins

@Test annotation in JUnit - Framework Patterns

Choose your learning style9 modes available
Framework Mode - @Test annotation
Folder Structure
project-root/
├── src/
│   ├── main/
│   │   └── java/
│   │       └── com/example/app/
│   │           └── App.java
│   └── test/
│       └── java/
│           └── com/example/app/
│               ├── pages/
│               │   └── LoginPage.java
│               ├── tests/
│               │   └── LoginTest.java
│               ├── utils/
│               │   └── TestUtils.java
│               └── config/
│                   └── TestConfig.java
├── pom.xml
└── README.md
    
Test Framework Layers
  • Test Classes: Contain test methods annotated with @Test. Example: LoginTest.java holds tests for login features.
  • Page Objects: Represent UI pages with methods to interact with elements. Example: LoginPage.java encapsulates login page actions.
  • Utilities: Helper methods for common tasks like waits, data generation, or assertions. Example: TestUtils.java.
  • Configuration: Manage environment settings, browser setup, and credentials. Example: TestConfig.java.
  • Build & Dependency Management: Managed by pom.xml using Maven for JUnit and Selenium dependencies.
Configuration Patterns

Use a dedicated config class or properties file to handle:

  • Environments: Define URLs for dev, test, and prod environments. Switch via system properties or profiles.
  • Browser Setup: Configure browser type (Chrome, Firefox) in TestConfig.java or via Maven profiles.
  • Credentials: Store securely using environment variables or encrypted files, accessed in tests via config utilities.
  • Timeouts & Waits: Centralize wait times and retry logic in utility classes.
Test Reporting and CI/CD Integration
  • JUnit generates XML reports by default, which CI tools like Jenkins or GitHub Actions can consume.
  • Use plugins like Surefire or Failsafe in Maven to run tests and generate reports.
  • Integrate with CI/CD pipelines to run tests automatically on code push or pull requests.
  • Use additional reporting tools like Allure for rich, user-friendly test reports.
Best Practices for @Test Annotation Framework
  • One Assertion per Test: Keep tests focused on a single behavior for clear pass/fail results.
  • Descriptive Test Names: Name test methods clearly to describe what they verify, e.g., loginWithValidCredentials_shouldSucceed().
  • Use Setup and Teardown: Use @BeforeEach and @AfterEach to prepare and clean test state.
  • Isolate Tests: Ensure tests do not depend on each other to avoid flaky results.
  • Use Assertions Properly: Use JUnit assertions like assertEquals, assertTrue to validate expected outcomes.
Self Check

Where in this folder structure would you add a new test method that verifies the logout functionality?

Key Result
Use @Test annotation in JUnit test classes under src/test/java to define executable test methods following clear structure and best practices.