From 3298d5fb960849c9ea17f0473169a92fe2136ae6 Mon Sep 17 00:00:00 2001
From: Mo Helmy <135069400+BenHelmyBen@users.noreply.github.com>
Date: Wed, 18 Oct 2023 04:59:54 +0300
Subject: [PATCH] Updating code related to the article BAEL-7017 (#15009)
* Update pom.xml
Update pom with the apache dependency
* Update WriteHashmaptoCVSFileUnitTest.java
Adding a new test method to solve the problem using Apache Commons CSV.
---
.../core-java-collections-maps-7/pom.xml | 7 +++-
.../WriteHashmaptoCVSFileUnitTest.java | 36 ++++++++++---------
2 files changed, 26 insertions(+), 17 deletions(-)
diff --git a/core-java-modules/core-java-collections-maps-7/pom.xml b/core-java-modules/core-java-collections-maps-7/pom.xml
index bb7c6e9fb5..bcc0915073 100644
--- a/core-java-modules/core-java-collections-maps-7/pom.xml
+++ b/core-java-modules/core-java-collections-maps-7/pom.xml
@@ -73,6 +73,11 @@
4.13.1
test
+
+ org.apache.commons
+ commons-csv
+ 1.5
+
-
\ No newline at end of file
+
diff --git a/core-java-modules/core-java-collections-maps-7/src/test/java/com/baeldung/map/writehashmaptocsvfile/WriteHashmaptoCVSFileUnitTest.java b/core-java-modules/core-java-collections-maps-7/src/test/java/com/baeldung/map/writehashmaptocsvfile/WriteHashmaptoCVSFileUnitTest.java
index ddebaf2468..e23a5da8ff 100644
--- a/core-java-modules/core-java-collections-maps-7/src/test/java/com/baeldung/map/writehashmaptocsvfile/WriteHashmaptoCVSFileUnitTest.java
+++ b/core-java-modules/core-java-collections-maps-7/src/test/java/com/baeldung/map/writehashmaptocsvfile/WriteHashmaptoCVSFileUnitTest.java
@@ -10,18 +10,26 @@ import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
+import org.apache.commons.csv.CSVFormat;
+import org.apache.commons.csv.CSVPrinter;
+
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class WriteHashmaptoCVSFileUnitTest {
+ public Map employeeData;
- @Test
- public void givenEmployeeData_whenWriteToCSV_thenCSVFileIsCreated() {
- Map employeeData = new HashMap<>();
+ public WriteHashmaptoCVSFileUnitTest() {
+ employeeData = new HashMap<>();
employeeData.put("Name", "John Doe");
employeeData.put("Title", "Software Engineer");
employeeData.put("Department", "Engineering");
employeeData.put("Salary", "75000");
+ }
+
+ @Test
+ public void givenEmployeeData_whenWriteToCSVUsingFileWriter_thenCSVFileIsCreated() {
+
try (FileWriter csvWriter = new FileWriter("employee_data.csv")) {
// Write header row
csvWriter.append("Name,Title,Department,Salary\n");
@@ -40,23 +48,19 @@ public class WriteHashmaptoCVSFileUnitTest {
}
@Test
- public void givenCSVFile_whenRead_thenContentsMatchExpected() {
- // Read the actual content of the CSV file
- StringBuilder actualCsvContent = new StringBuilder();
- try {
- Files.lines(Paths.get("employee_data.csv"))
- .forEach(line -> actualCsvContent.append(line).append("\n"));
+ public void givenCSVFile_whenWriteToCSVUsingApacheCommons_thenContentsMatchExpected() {
- // Define the expected CSV content
- String expectedCsvContent = "Name,Title,Department,Salary\n" +
- "John Doe,Software Engineer,Engineering,75000\n";
+ try (CSVPrinter csvPrinter = new CSVPrinter(new FileWriter("employee_data2.csv"), CSVFormat.DEFAULT)) {
+ // Write header row
+ csvPrinter.printRecord("Name", "Title", "Department", "Salary");
- // Compare the actual content with the expected content
- assertEquals(expectedCsvContent, actualCsvContent.toString());
-
- System.out.println("CSV file created successfully.");
+ // Write data row
+ csvPrinter.printRecord(employeeData.get("Name"), employeeData.get("Title"), employeeData.get("Department"), employeeData.get("Salary"));
} catch (IOException e) {
e.printStackTrace();
}
+
+ // Ensure the CSV file exists
+ assertTrue(new File("employee_data2.csv").exists());
}
}