Parameterized Test

About

A @ParameterizedTest is a test method in JUnit 5 that runs multiple times with different arguments. Instead of duplicating test code with different input values, parameterized tests allow you to write a single test method that automatically receives different input values and runs multiple times.

This improves test readability and maintainability.

Why Use Parameterized Tests?

  • Reduce duplication in test code.

  • Cover more input cases with less effort.

  • Improve test coverage and reliability.

1. @ValueSource – Single Literal Values

Use @ValueSource when we want to run a test multiple times with a single argument of the same type (e.g., all integers or all strings). It is ideal for primitive or simple input validation.

Basic Usage

@ParameterizedTest
@ValueSource(ints = {1, 2, 3})
void testWithInt(int number) {
    assertTrue(number > 0);
}

Advanced Usage

  • Supported types: int, long, double, short, byte, char, boolean, String, Class<?>

  • Limitation: Only one parameter is allowed.

  • Passing null: Not supported directly. You must use @NullSource or @NullAndEmptySource.

Handling Nulls and Edge Cases

  • @NullSource → adds one null argument.

  • @EmptySource → adds empty value ("", empty collection/array).

2. @CsvSource

Use @CsvSource to test methods with multiple parameters. The values are given inline in a CSV format. It improves clarity when testing combinations of inputs.

Inline CSV Data

Basic Usage

Advanced Usage

  • String values with commas: Use quotes.

  • Null values: Use null as a literal string (quotes are required).

Load Data from CSV File

Basic Usage

Advanced Usage

  • File format: CSV file must be on the classpath.

  • Quotes can be used to handle commas inside values.

  • Skipping header: Use numLinesToSkip = 1.

  • Custom delimiters: Use delimiter = ';' for semi-colon delimited files.

4. @EnumSource – Use Enum Constants

Use @EnumSource when testing logic based on different enum values. It ensures all or selected enum constants are covered.

Basic Usage

Advanced Usage

  • Include specific constants:

  • Exclude constants using regex:

5. @MethodSource – Arguments from a Factory Method

Use @MethodSource when test arguments are dynamic, complex, or require logic to generate. It supports multiple parameters and null.

Basic Usage

Multiple Parameters

Advanced Usage

  • Nullable arguments: We can pass null safely.

  • Dynamic generation using files, DB, or API:

6. @ArgumentsSource – Custom Provider

Use @ArgumentsSource when you want to provide parameters from a custom provider class. It's useful for reading from a DB, API, or complex logic.

Basic Usage

Example 1: Hardcoded List of Strings

Example 2: Providing Multiple Parameters

Example 3: Random Data Generator

Example 4: Reading from a File

Example 5: Generating Null and Edge Cases

Advanced Usage

  • Useful when input comes from:

    • External services

    • Complex business logic

    • Random generators

  • We can inject dependencies into the provider if needed via Spring or custom mechanisms.

Last updated