Test Overview
This test reads multiple sets of input data from an external CSV file using @CsvFileSource. It verifies that the sum of two numbers equals the expected result for each data row.
This test reads multiple sets of input data from an external CSV file using @CsvFileSource. It verifies that the sum of two numbers equals the expected result for each data row.
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvFileSource; import static org.junit.jupiter.api.Assertions.assertEquals; public class CalculatorTest { @ParameterizedTest @CsvFileSource(resources = "/data/sum-data.csv", numLinesToSkip = 1) void testSum(int a, int b, int expectedSum) { int actualSum = a + b; assertEquals(expectedSum, actualSum, () -> String.format("Sum of %d and %d should be %d", a, b, expectedSum)); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test runner starts and loads the test class CalculatorTest | JUnit test environment initialized | — | PASS |
| 2 | Reads the CSV file '/data/sum-data.csv' skipping the header line | CSV file content loaded: rows of integers for a, b, expectedSum | — | PASS |
| 3 | Executes testSum with first data row (a=1, b=2, expectedSum=3) | Test method invoked with parameters (1, 2, 3) | Check if 1 + 2 equals 3 | PASS |
| 4 | Executes testSum with second data row (a=5, b=7, expectedSum=12) | Test method invoked with parameters (5, 7, 12) | Check if 5 + 7 equals 12 | PASS |
| 5 | Executes testSum with third data row (a=10, b=15, expectedSum=25) | Test method invoked with parameters (10, 15, 25) | Check if 10 + 15 equals 25 | PASS |
| 6 | All parameterized test cases complete | Test report generated showing all tests passed | — | PASS |
@CsvFileSource in JUnit tests?@CsvFileSource?numLinesToSkip.numLinesToSkip = 1 which skips the first line (header).data.csv with content:name,age Alice,30 Bob,25
@ParameterizedTest
@CsvFileSource(resources = "/data.csv", numLinesToSkip = 1)
void testUser(String name, int age) {
System.out.println(name + "-" + age);
}@ParameterizedTest
@CsvFileSource(resources = "/users.csv")
void testUser(String name, int age) {
assertTrue(age > 0);
}users.csv has a header line. What is the likely problem when running this test?numLinesToSkip is set, so header line is treated as data.product,price,discountRate. The CSV has a header line. Which is the best way to write the parameterized test method signature and annotation to correctly read and use this data?numLinesToSkip = 1 to skip the CSV header line.double for price and discountRate.numLinesToSkip=1 and matching parameter types. @ParameterizedTest
@CsvFileSource(resources = "/discounts.csv")
void testDiscount(String product, int price, int discountRate) { ... } misses skipping header and uses int instead of double. @ParameterizedTest
@CsvFileSource(resources = "/discounts.csv", numLinesToSkip = 1)
void testDiscount(String product, String price, String discountRate) { ... } uses String for numeric fields. @ParameterizedTest
@CsvFileSource(resources = "/discounts.csv", skipHeader = true)
void testDiscount(String product, double price, double discountRate) { ... } uses invalid attribute skipHeader.Pass the test and this pattern moves unknown