0
0
JUnittesting~15 mins

Why parameterization reduces test duplication in JUnit - Automation Benefits in Action

Choose your learning style9 modes available
Verify login functionality with multiple user credentials using parameterization
Preconditions (2)
Step 1: Open the login page
Step 2: Enter username from the test data
Step 3: Enter password from the test data
Step 4: Click the login button
Step 5: Verify that the user is redirected to the dashboard page
✅ Expected Result: User is successfully logged in and redirected to the dashboard page for each set of credentials
Automation Requirements - JUnit 5
Assertions Needed:
Verify the current URL is the dashboard URL after login
Verify no error message is displayed after login
Best Practices:
Use @ParameterizedTest with @CsvSource for input data
Use assertions from org.junit.jupiter.api.Assertions
Keep test method focused on one behavior
Avoid hardcoding test data inside the test method
Automated Solution
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class LoginTest {

    WebDriver driver;

    @ParameterizedTest
    @CsvSource({
        "user1@example.com, Password1",
        "user2@example.com, Password2",
        "user3@example.com, Password3"
    })
    void testLoginWithMultipleUsers(String username, String password) {
        driver = new ChromeDriver();
        try {
            driver.get("http://example.com/login");

            driver.findElement(By.id("email")).sendKeys(username);
            driver.findElement(By.id("password")).sendKeys(password);
            driver.findElement(By.id("loginButton")).click();

            // Wait for redirection - simple implicit wait or explicit wait can be used
            Thread.sleep(2000); // For simplicity here, better to use explicit wait in real tests

            String currentUrl = driver.getCurrentUrl();
            assertEquals("http://example.com/dashboard", currentUrl, "User should be redirected to dashboard");

            boolean errorDisplayed = driver.findElements(By.id("loginError")).size() > 0;
            assertFalse(errorDisplayed, "No error message should be displayed after successful login");

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            driver.quit();
        }
    }
}

This test uses JUnit 5's @ParameterizedTest with @CsvSource to run the same login test with different username and password combinations.

Each test iteration opens the login page, enters the provided credentials, clicks login, and verifies the user is redirected to the dashboard URL.

Assertions check the URL and that no error message is shown.

Parameterization reduces duplication by avoiding writing separate test methods for each user credential set.

The try-finally block ensures the browser closes after each test run.

Note: Thread.sleep is used here for simplicity but explicit waits are recommended in real tests.

Common Mistakes - 4 Pitfalls
Writing separate test methods for each set of credentials
Hardcoding test data inside the test method body
Not closing the browser after each test iteration
Using Thread.sleep instead of explicit waits
Bonus Challenge

Now add data-driven testing with 3 different inputs using @MethodSource instead of @CsvSource

Show Hint