How do I use @CsvSource in JUnit parameterized tests?

To use @CsvSource in JUnit 5 parameterized tests, you define multiple sets of comma-separated input values directly inside the annotation. Each CSV row becomes one test invocation.

1. Add the Required Imports

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.junit.jupiter.api.Assertions.assertEquals;

@CsvSource is part of JUnit Jupiter Params, so make sure your project includes the parameterized test dependency.

For Maven:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-params</artifactId>
    <version>5.11.0</version>
    <scope>test</scope>
</dependency>

For Gradle:

testImplementation 'org.junit.jupiter:junit-jupiter-params:5.11.0'

2. Basic @CsvSource Example

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.junit.jupiter.api.Assertions.assertEquals;

class CalculatorTest {

    @ParameterizedTest
    @CsvSource({
        "1, 2, 3",
        "5, 7, 12",
        "10, 20, 30"
    })
    void shouldAddTwoNumbers(int a, int b, int expected) {
        int result = a + b;

        assertEquals(expected, result);
    }
}

Each line in @CsvSource maps to the method parameters:

"1, 2, 3"  -> a = 1, b = 2, expected = 3
"5, 7, 12" -> a = 5, b = 7, expected = 12

3. Using Strings with @CsvSource

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.junit.jupiter.api.Assertions.assertEquals;

class StringTest {

    @ParameterizedTest
    @CsvSource({
        "java, JAVA",
        "junit, JUNIT",
        "test, TEST"
    })
    void shouldConvertTextToUppercase(String input, String expected) {
        assertEquals(expected, input.toUpperCase());
    }
}

JUnit automatically converts CSV values to the method parameter types when possible.

4. Handling Commas in Values

If a value contains a comma, wrap it in single quotes:

@ParameterizedTest
@CsvSource({
    "'Smith, John', John",
    "'Doe, Jane', Jane"
})
void shouldExtractFirstName(String fullName, String firstName) {
    String result = fullName.substring(fullName.indexOf(",") + 2);

    assertEquals(firstName, result);
}

Here, 'Smith, John' is treated as one argument instead of two.

5. Using Custom Delimiters

By default, @CsvSource uses a comma. You can change the delimiter:

@ParameterizedTest
@CsvSource(
    value = {
        "1|2|3",
        "4|5|9",
        "10|20|30"
    },
    delimiter = '|'
)
void shouldAddNumbersWithCustomDelimiter(int a, int b, int expected) {
    assertEquals(expected, a + b);
}

6. Null and Empty Values

JUnit treats an unquoted empty value as null:

@ParameterizedTest
@CsvSource({
    "apple, APPLE",
    ", UNKNOWN"
})
void shouldHandleNullValues(String input, String expected) {
    String result = input == null ? "UNKNOWN" : input.toUpperCase();

    assertEquals(expected, result);
}

You can use an empty string by quoting it:

@ParameterizedTest
@CsvSource({
    "'', empty"
})
void shouldHandleEmptyString(String input, String expected) {
    String result = input.isEmpty() ? "empty" : input;

    assertEquals(expected, result);
}

7. Adding Display Names

You can make test output easier to read using the name attribute:

@ParameterizedTest(name = "{index} => input={0}, expected={1}")
@CsvSource({
    "java, JAVA",
    "junit, JUNIT"
})
void shouldConvertToUppercase(String input, String expected) {
    assertEquals(expected, input.toUpperCase());
}

Example display names:

1 => input=java, expected=JAVA
2 => input=junit, expected=JUNIT

Key Points

  • Use @ParameterizedTest, not @Test.
  • Use @CsvSource to provide multiple comma-separated argument sets.
  • Each CSV row must match the number of method parameters.
  • JUnit automatically converts values to types like int, double, boolean, String, and enums.
  • Use single quotes for values containing commas.
  • Use quoted empty strings for ""; unquoted empty values are treated as null.
  • Use delimiter if comma-separated data is hard to read.

How do I write parameterized tests in JUnit?

In JUnit 5, parameterized tests let you run the same test multiple times with different input values.

You use:

@ParameterizedTest

instead of:

@Test

Then you provide test data using a source annotation such as @ValueSource, @CsvSource, @MethodSource, or @EnumSource.

Basic Example with @ValueSource

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.assertTrue;

class NumberTest {

    @ParameterizedTest
    @ValueSource(ints = {1, 2, 5, 10})
    void shouldBePositive(int number) {
        assertTrue(number > 0);
    }
}

This runs the test 4 times:

number = 1
number = 2
number = 5
number = 10

Strings with @ValueSource

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.assertFalse;

class StringTest {

    @ParameterizedTest
    @ValueSource(strings = {"hello", "junit", "test"})
    void shouldNotBeBlank(String value) {
        assertFalse(value.isBlank());
    }
}

@ValueSource supports simple values such as:

@ValueSource(strings = {"a", "b"})
@ValueSource(ints = {1, 2, 3})
@ValueSource(longs = {1L, 2L})
@ValueSource(doubles = {1.5, 2.5})
@ValueSource(booleans = {true, false})

Multiple Arguments with @CsvSource

Use @CsvSource when each test case needs more than one value.

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.junit.jupiter.api.Assertions.assertEquals;

class CalculatorTest {

    @ParameterizedTest
    @CsvSource({
            "1, 2, 3",
            "5, 7, 12",
            "10, -2, 8"
    })
    void shouldAddNumbers(int a, int b, int expected) {
        int result = a + b;

        assertEquals(expected, result);
    }
}

Each CSV row maps to the test method parameters:

a, b, expected

Use @NullSource, @EmptySource, and @NullAndEmptySource

These are useful for testing validation logic.

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.assertTrue;

class UsernameValidatorTest {

    @ParameterizedTest
    @NullAndEmptySource
    @ValueSource(strings = {" ", "   "})
    void shouldRejectBlankUsernames(String username) {
        assertTrue(username == null || username.isBlank());
    }
}

This test runs with:

null
""
" "
"   "

You can also use them separately:

@NullSource
@EmptySource
@NullAndEmptySource

Use @MethodSource for Complex Test Data

Use @MethodSource when test data is more complex or easier to build in Java code.

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;

class DiscountCalculatorTest {

    @ParameterizedTest
    @MethodSource("discountCases")
    void shouldCalculateDiscount(int price, double discountRate, int expectedPrice) {
        int result = (int) (price * (1 - discountRate));

        assertEquals(expectedPrice, result);
    }

    static Stream<Arguments> discountCases() {
        return Stream.of(
                Arguments.of(100, 0.10, 90),
                Arguments.of(200, 0.25, 150),
                Arguments.of(80, 0.50, 40)
        );
    }
}

The method source is usually static, unless the test class uses a per-class test instance lifecycle.

Use @EnumSource for Enum Values

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

import static org.junit.jupiter.api.Assertions.assertNotNull;

class RoleTest {

    enum Role {
        ADMIN,
        USER,
        GUEST
    }

    @ParameterizedTest
    @EnumSource(Role.class)
    void shouldHaveValidRole(Role role) {
        assertNotNull(role);
    }
}

You can also include or exclude specific enum values:

@EnumSource(value = Role.class, names = {"ADMIN", "USER"})

or:

@EnumSource(
        value = Role.class,
        names = {"GUEST"},
        mode = EnumSource.Mode.EXCLUDE
)

Naming Each Parameterized Test Run

You can customize the displayed name for each invocation:

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.junit.jupiter.api.Assertions.assertEquals;

class CalculatorTest {

    @ParameterizedTest(name = "{0} + {1} should equal {2}")
    @CsvSource({
            "1, 2, 3",
            "5, 7, 12",
            "10, -2, 8"
    })
    void shouldAddNumbers(int a, int b, int expected) {
        assertEquals(expected, a + b);
    }
}

This can show test names like:

1 + 2 should equal 3
5 + 7 should equal 12
10 + -2 should equal 8

Common placeholders include:

{index}    the invocation index
{0}        the first argument
{1}        the second argument
{arguments} all arguments

Required Dependency

For Maven:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.13.4</version>
    <scope>test</scope>
</dependency>

For Gradle:

testImplementation 'org.junit.jupiter:junit-jupiter:5.13.4'

test {
    useJUnitPlatform()
}

Quick Rule of Thumb

Use:

  • @ValueSource for one simple argument
  • @CsvSource for several simple arguments
  • @MethodSource for complex objects or generated cases
  • @EnumSource for enum values
  • @NullSource, @EmptySource, or @NullAndEmptySource for null/empty validation cases

A typical parameterized test looks like this:

@ParameterizedTest
@CsvSource({
        "2, 3, 5",
        "10, 5, 15"
})
void shouldAddNumbers(int a, int b, int expected) {
    assertEquals(expected, a + b);
}