0
0
JUnittesting~15 mins

@CsvFileSource for external CSV in JUnit - Build an Automation Script

Choose your learning style9 modes available
Automate parameterized test using @CsvFileSource with external CSV file
Preconditions (3)
Step 1: Create a parameterized test method annotated with @ParameterizedTest
Step 2: Use @CsvFileSource to load data from 'test-data.csv'
Step 3: For each row, receive the input string and expected length as parameters
Step 4: Assert that the length of the input string equals the expected length
✅ Expected Result: The test runs once per CSV row and passes if all string lengths match the expected values
Automation Requirements - JUnit 5
Assertions Needed:
Assert that input string length equals expected length
Best Practices:
Use @ParameterizedTest with @CsvFileSource for clean data-driven tests
Place CSV file in 'src/test/resources' for easy classpath access
Use meaningful assertion messages
Keep test method simple and focused
Automated Solution
JUnit
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class CsvFileSourceTest {

    @ParameterizedTest
    @CsvFileSource(resources = "/test-data.csv", numLinesToSkip = 1)
    void testStringLength(String input, int expectedLength) {
        assertEquals(expectedLength, input.length(),
            () -> "Expected length " + expectedLength + " but got " + input.length() + " for input: '" + input + "'");
    }
}

The test class CsvFileSourceTest uses JUnit 5's @ParameterizedTest to run the test method multiple times with different inputs.

The @CsvFileSource annotation loads data from the external CSV file test-data.csv located in src/test/resources. The resources path starts with a slash to indicate classpath root.

numLinesToSkip = 1 skips the header row in the CSV file.

The test method testStringLength receives two parameters: the input string and the expected length from each CSV row.

Inside the method, assertEquals checks if the actual string length matches the expected length, with a clear message to help debug if it fails.

This approach keeps tests clean, readable, and easy to maintain.

Common Mistakes - 4 Pitfalls
{'mistake': 'Placing the CSV file outside the classpath or wrong folder', 'why_bad': 'JUnit cannot find the CSV file, causing test failures or errors', 'correct_approach': "Place the CSV file inside 'src/test/resources' so it is on the classpath"}
{'mistake': 'Not skipping the header line in the CSV file', 'why_bad': 'The header row is treated as test data, causing parsing errors or wrong test inputs', 'correct_approach': "Use 'numLinesToSkip = 1' in @CsvFileSource to skip the header"}
Mismatch between CSV columns and test method parameters
{'mistake': 'Using relative file paths instead of classpath resource paths', 'why_bad': 'JUnit cannot locate the file if path is incorrect', 'correct_approach': "Use resource path starting with '/' to load from classpath"}
Bonus Challenge

Extend the test to also verify that the input string is not empty for each CSV row

Show Hint