Sure! If you're working with Java and CSV files for testing purposes, the CSVParser class (typically from libraries like Apache Commons CSV ) can be very useful to read and parse CSV files in unit tests or integration tests.
Here are a few examples using Apache Commons CSV to demonstrate how to use CSVParser for Java testing. If you're not yet using it, you can add the Maven dependency like this:
✅ Maven Dependency for Apache Commons CSV
xml Copy code
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.10.0</version> <!-- Use the latest version available -->
</dependency>
📘 Example 1: Parse a Sample CSV String
This is useful for unit tests without needing actual files.
java Copy code
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import java.io.StringReader;
import java.util.List;
public class CSVParserExample {
public static void main(String[] args) throws Exception {
String csvData = "Name, Age, Country\n"
+ "Alice, 30, USA\n"
+ "Bob, 25, UK";
CSVParser parser = CSVParser.parse(
new StringReader(csvData),
CSVFormat.DEFAULT.withFirstRecordAsHeader().withTrim()
);
for (CSVRecord record : parser) {
System.out.println("Name: " + record.get("Name"));
System.out.println("Age: " + record.get("Age"));
System.out.println("Country: " + record.get("Country"));
}
}
}
📘 Example 2: Load CSV for Testing from Classpath
You might have a file like test-data.csv under src/test/resources.
📄 Example test-data.csv:
csv Copy code
ID, Product, Price
1, Laptop, 1200
2, Mouse, 25
🔍 Load CSV in a test:
java Copy code
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.junit.jupiter.api.Test;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ProductRepositoryTest {
@Test
public void testLoadProductsFromCSV() throws Exception {
try (InputStreamReader reader = new InputStreamReader(
getClass().getResourceAsStream("/test-data.csv"), StandardCharsets.UTF_8)) {
CSVParser parser = CSVFormat.DEFAULT
.withFirstRecordAsHeader()
.withTrim()
.parse(reader);
List<CSVRecord> records = parser.getRecords();
assertEquals(2, records.size());
assertEquals("Laptop", records.get(0).get("Product"));
assertEquals("25", records.get(1).get("Price"));
}
}
}
📘 Example 3: Parameterized JUnit 5 Tests with CSV
JUnit 5 supports parameterized tests with CSV source:
java Copy code
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class MathTest {
@ParameterizedTest
@CsvSource({
"1, 1, 2",
"2, 3, 5",
"10, 5, 15"
})
void testAddition(int a, int b, int expectedSum) {
assertEquals(expectedSum, a + b);
}
}
✅ Summary
What How In-memory string CSV Use StringReader with CSVParser File-based CSV (e.g., for integration tests) Load from resources using InputStreamReader JUnit parameterized CSV tests Use @CsvSource or @CsvFileSource Header handling Use .withFirstRecordAsHeader() Whitespace trimming Use .withTrim()
Let me know if you'd like examples using other libraries like OpenCSV or Jackson for CSV parsing instead.