This project demonstrates how to write and run unit tests using Java, JUnit 5, and Selenium WebDriver with LambdaTest's cloud platform. It includes examples of form testing, calculator functionality, and cross-browser testing.
- Features
- Prerequisites
- Setup
- Project Structure
- Running Tests
- Viewing Test Results
- Test Types
- Configuration
- Contributing
- Cross-browser testing (Chrome, Firefox, Safari, Edge)
- Parallel test execution
- Parameterized tests
- Cloud-based test execution with LambdaTest
- Local test execution support
- Configurable test settings
- Java JDK 21 or higher
- Maven
- LambdaTest Account
- Sign up at LambdaTest
- Get your username and access key from LambdaTest Security Settings
-
Clone the repository:
git clone https://github.com/yourusername/java-unit-testing-guide.git cd java-unit-testing-guide
-
Set up LambdaTest credentials:
- For Linux/Mac:
export LT_USERNAME=your_username export LT_ACCESS_KEY=your_access_key
- For Windows:
set LT_USERNAME=your_username set LT_ACCESS_KEY=your_access_key
- For Linux/Mac:
-
Install dependencies:
mvn clean install
java-unit-testing-guide/
├── src/
│ ├── main/java/
│ │ └── com/lambdatest/tutorial/
│ │ └── Calculator.java
│ └── test/java/
│ └── com/lambdatest/tutorial/
│ ├── config/
│ │ └── TestConfig.java
│ ├── util/
│ │ ├── BaseTest.java
│ │ └── WebDriverUtil.java
│ ├── CloudCalculatorTest.java
│ ├── LocalSeleniumCalculatorTest.java
│ └── CalculatorTest.java
└── pom.xml
mvn test
mvn test -Dtest=CloudCalculatorTest
mvn test -Dtest=CloudCalculatorTest#testWithDifferentBrowsers
- Log in to LambdaTest Automation Builds
- Find your build using the format: "Calculator Tests - YYYY-MM-DD HH:mm:ss"
The repository includes screenshots of test runs demonstrating the cross-browser testing capabilities:
-
lambdatest_tests_run.png
- Shows the test execution results across multiple browsers (Chrome, Firefox, Edge, Safari)
- Demonstrates parallel test execution with 5 concurrent tests
- Highlights the build naming convention with timestamps
- Shows test status and execution time for each browser configuration
-
junit_tests_status.png
- Displays the detailed JUnit test execution results
- Shows test statistics including total tests run, failures, errors, and skipped tests
- Includes execution time for each test class
- Demonstrates the test organization and naming conventions used in the project
Test reports are generated in:
target/surefire-reports/
- Pure unit tests for calculator logic
- No browser dependency
- Fast execution
- Selenium tests running on local machine
- Chrome browser only
- Good for quick local testing
- Cross-browser testing on LambdaTest platform
- Tests calculator functionality across different browsers
- Parallel execution support
- Build naming format: "Calculator Tests - {timestamp}"
The TestConfig
class centralizes all test configuration settings:
- LambdaTest Credentials: Environment variables for username and access key
- Application URL: The target website for testing
- Build Configuration: Format for build names and timestamps
- Browser Settings: Supported browsers (Chrome, Firefox, Safari, Edge)
- Version Settings: Browser versions (latest, specific versions)
- Platform Settings: Operating systems (Windows 10, macOS Monterey)
- Test Status: Constants for test results (passed, failed)
junit.jupiter.execution.parallel.enabled=true
junit.jupiter.execution.parallel.mode.default=concurrent
junit.jupiter.execution.parallel.mode.classes.default=concurrent
junit.jupiter.execution.parallel.config.strategy=fixed
junit.jupiter.execution.parallel.config.fixed.parallelism=5
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
)