0
0
JUnittesting~8 mins

assertDoesNotThrow in JUnit - Framework Patterns

Choose your learning style9 modes available
Framework Mode - assertDoesNotThrow
Folder Structure
src/
└── test/
    └── java/
        └── com/
            └── example/
                ├── pages/
                │   └── LoginPage.java
                ├── tests/
                │   └── LoginTests.java
                ├── utils/
                │   └── TestUtils.java
                └── config/
                    └── TestConfig.java
Test Framework Layers
  • Config Layer: Holds environment settings, browser configs, credentials (e.g., TestConfig.java).
  • Page Object Layer: Represents UI pages with methods to interact (e.g., LoginPage.java).
  • Test Layer: Contains test classes with test methods using JUnit assertions like assertDoesNotThrow (e.g., LoginTests.java).
  • Utility Layer: Helper methods and reusable code (e.g., TestUtils.java).
Configuration Patterns
  • Environment Properties: Use TestConfig.java to load environment variables (dev, test, prod) via System.getProperty or config files.
  • Browser Setup: Configure WebDriver in setup methods, selecting browser type from config.
  • Credentials Management: Store sensitive data securely, inject into tests via config or environment variables.
  • Timeouts and Waits: Centralize timeout values in config for consistency.
Test Reporting and CI/CD Integration
  • JUnit Reports: Use built-in JUnit XML reports for test results.
  • Enhanced Reporting: Integrate with tools like Allure or Surefire for detailed reports.
  • CI/CD Integration: Run tests automatically on code push using Jenkins, GitHub Actions, or GitLab CI.
    • Fail build if assertDoesNotThrow detects an exception.
    • Publish reports as build artifacts.
Best Practices
  1. Use assertDoesNotThrow to confirm code runs without exceptions. It makes tests clear and focused on error absence.
  2. Keep tests small and focused. Test one behavior per test method.
  3. Use Page Object Model. Separate UI logic from tests for easier maintenance.
  4. Centralize configuration. Avoid hardcoding values inside tests.
  5. Integrate tests into CI/CD. Catch failures early and keep feedback fast.
Self Check

Where would you add a new test method that uses assertDoesNotThrow to verify login functionality?

Key Result
Organize JUnit tests with clear layers and use assertDoesNotThrow to verify no exceptions occur during execution.