0
0
JUnittesting~8 mins

@ValueSource for simple values in JUnit - Framework Patterns

Choose your learning style9 modes available
Framework Mode - @ValueSource for simple values
Folder Structure
src/
└── test/
    └── java/
        └── com/
            └── example/
                ├── tests/
                │   └── CalculatorTests.java
                ├── pages/
                │   └── CalculatorPage.java
                ├── utils/
                │   └── TestUtils.java
                └── config/
                    └── TestConfig.java
    

This structure organizes test code, page objects, utilities, and configuration separately.

Test Framework Layers
  • Test Layer: Contains test classes using JUnit annotations like @Test and @ParameterizedTest with @ValueSource for simple value inputs.
  • Page Object Layer: Encapsulates UI elements and actions for maintainability.
  • Utility Layer: Helper methods for common tasks like data formatting or waits.
  • Configuration Layer: Manages environment settings, browser options, and credentials.
Configuration Patterns

Use TestConfig.java or application.properties to store environment variables like URLs and credentials.

Example: Use system properties or environment variables to select browser or environment at runtime.

// Example snippet in TestConfig.java
public class TestConfig {
    public static final String BASE_URL = System.getProperty("baseUrl", "https://default.example.com");
    public static final String BROWSER = System.getProperty("browser", "chrome");
}
    
Test Reporting and CI/CD Integration
  • Use JUnit's built-in reports or integrate with tools like Surefire or Allure for detailed test reports.
  • Configure CI/CD pipelines (e.g., Jenkins, GitHub Actions) to run tests automatically on code changes.
  • Reports show which @ValueSource inputs passed or failed, helping quickly identify failing data cases.
Best Practices
  • Use @ValueSource for simple, single-parameter inputs like strings, ints, or enums to keep tests clean and readable.
  • Keep test methods focused on one behavior and use descriptive names for clarity.
  • Combine @ValueSource with @ParameterizedTest to avoid code duplication for similar test cases.
  • Validate all input values with assertions to ensure test reliability.
  • Organize test data logically and avoid hardcoding values inside test methods.
Self Check

Where in this folder structure would you add a new test class that uses @ValueSource to test different input values for a login feature?

Key Result
Use @ValueSource with @ParameterizedTest in the test layer to run simple input-driven tests cleanly and efficiently.