@CsvSource lets you run the same test many times with different data. It saves time and avoids repeating code.
@CsvSource for inline CSV data in JUnit
@ParameterizedTest
@CsvSource({"value1, value2, expectedResult", "value3, value4, expectedResult2"})
void testMethod(type param1, type param2, type expected) {
// test code
}Each string inside @CsvSource represents one test case with comma-separated values.
Values are passed as parameters to the test method in the same order.
@ParameterizedTest
@CsvSource({"1, 2, 3", "4, 5, 9"})
void addTest(int a, int b, int expected) {
assertEquals(expected, a + b);
}@ParameterizedTest
@CsvSource({"apple, 5", "banana, 6"})
void lengthTest(String word, int length) {
assertEquals(length, word.length());
}@ParameterizedTest
@CsvSource({"true, TRUE", "false, FALSE"})
void caseTest(boolean flag, String text) {
assertEquals(text, String.valueOf(flag).toUpperCase());
}This test runs three times with different numbers. It checks if adding two numbers equals the expected result.
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; public class CalculatorTest { @ParameterizedTest @CsvSource({"1, 2, 3", "10, 20, 30", "5, 7, 12"}) void testAdd(int a, int b, int expected) { assertEquals(expected, a + b); } }
Use quotes around values if they contain spaces or special characters.
Empty strings can be represented by double quotes "" inside the CSV data.
Keep the number of CSV values and method parameters the same to avoid errors.
@CsvSource helps run one test many times with different data.
It keeps test data inside the test code for easy reading.
Use it for small, simple sets of test data without external files.
Practice
What is the main purpose of using @CsvSource in JUnit tests?
Solution
Step 1: Understand the role of @CsvSource
@CsvSource allows specifying multiple sets of input data inline for a single test method.Step 2: Identify the purpose in testing
This lets the test run repeatedly with each data set, checking behavior for different inputs.Final Answer:
To run the same test multiple times with different input data -> Option AQuick Check:
@CsvSource runs tests with multiple inputs [OK]
- Thinking @CsvSource connects to databases
- Confusing it with random data generation
- Assuming it creates UI elements
Which of the following is the correct syntax to use @CsvSource for a test method with two parameters int a and int b?
@ParameterizedTest
@CsvSource({"1, 2", "3, 4"})
void testSum(int a, int b) { ... }Solution
Step 1: Check the delimiter used in @CsvSource
The default delimiter for @CsvSource is a comma (,), so values should be separated by commas.Step 2: Match the syntax with parameters
Since the method expects two int parameters, each CSV line must have two comma-separated values.Final Answer:
@CsvSource({"1, 2", "3, 4"}) -> Option BQuick Check:
Comma separates values in @CsvSource [OK]
- Using semicolons or spaces instead of commas
- Using wrong delimiters like |
- Not matching parameter count with CSV values
What will be the output of the following JUnit test when run with @CsvSource?
@ParameterizedTest
@CsvSource({"2, 3, 5", "4, 5, 9", "1, 1, 3"})
void testAdd(int a, int b, int expected) {
assertEquals(expected, a + b);
}Solution
Step 1: Evaluate each test case
Test 1: 2 + 3 = 5 matches expected 5 (pass) Test 2: 4 + 5 = 9 matches expected 9 (pass) Test 3: 1 + 1 = 2 does not match expected 3 (fail)Step 2: Determine overall result
Since one test fails, the test suite reports failure for that case.Final Answer:
The last test fails -> Option DQuick Check:
Check sums against expected values [OK]
- Assuming all tests pass without checking expected values
- Ignoring the last test case
- Confusing parameter order
Identify the error in this JUnit test using @CsvSource:
@ParameterizedTest
@CsvSource({"apple, 1", "banana"})
void testFruitCount(String fruit, int count) {
assertTrue(count > 0);
}Solution
Step 1: Check CSV lines against method parameters
The method expects two parameters: a String and an int. The first CSV line has two values, but the second line has only one.Step 2: Understand the impact of missing value
Missing the second value in the second line causes a runtime error because the test cannot map parameters properly.Final Answer:
Missing second value in second CSV line causes error -> Option CQuick Check:
Each CSV line must have values matching parameters [OK]
- Ignoring missing values in CSV lines
- Assuming assertTrue usage causes error
- Thinking @CsvSource limits lines to one
You want to test a method isAdult(int age) that returns true if age is 18 or more, false otherwise. Which @CsvSource usage correctly tests ages 17, 18, and 20 with expected results?
Solution
Step 1: Use correct delimiter for @CsvSource
The default delimiter is a comma, so values must be comma-separated.Step 2: Match test data with method parameters
Each line has an int age and a boolean expected result, correctly formatted with commas.Final Answer:
@CsvSource({"17, false", "18, true", "20, true"}) -> Option AQuick Check:
Comma-separated values match parameters and expected results [OK]
- Using semicolons or spaces instead of commas
- Using wrong delimiters like |
- Not matching parameter count or types
Ready to prove it?
Pass the test and this pattern moves unknown
