'how to coverage write file func in java
I made a simple function? to which I give a list of objects and then get their headers (just some strings), then sort and write to a file when i made a test for this func, coverage was 0 lines and i dont know why
public static void writeToFile(List<Appliances> appliances) throws IOException {
try (FileWriter writer = new FileWriter("\\result.csv")) {
appliances.stream().sorted(Comparator.comparing(Appliances::getName)).collect(Collectors.toList());
String previousClassName = "";
for (var app : appliances) {
if (!app.getClass().getSimpleName().equals(previousClassName)) {
writer.write(app.getHeaders());
writer.write("\r\n");
writer.write(app.toCSV());
writer.write("\r\n");
previousClassName = app.getClass().getSimpleName();
}
}
}
}
so here is my unit test code, also I'm not native english speaker, dont write clever words pls, just a code and a little text would be a perfect solution, thanks a lot
@Test
void writeToFile() throws Exception {
try (FileReader expected = new FileReader(String.valueOf(path));
BufferedReader expectedBR = new BufferedReader(expected);
FileReader result = new FileReader(String.valueOf(path2));
BufferedReader resultBR = new BufferedReader(result);
) {
String line1;
String line2;
while ((line1 = expectedBR.readLine()) != null & (line2 = resultBR.readLine()) != null) {
Assertions.assertEquals(line1, line2);
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
