Reputation: 49
I have a following save method, but I dont know how to verify this method. How can i verify it in JUnit ??
public static void save(Spiel spielen,File file ) {
try(ObjectOutputStream out= new ObjectOutputStream(new FileOutputStream(file))) {
out.writeObject(spielen);
System.out.println("Speichern Erfolgreich");
System.out.println();
}
catch (Exception e) {
System.out.println("Fehler beim Speichern");
System.out.println();
}
}
Upvotes: 0
Views: 61
Reputation: 1319
You can store the reference, expected output file on the disk, and then compare the tested output against that. There are many ways to do that comparison, including some JUnit Addons (its FileAssert in particular), or just read both files into byte arrays and assert that they equal.
Many other utilities exist, some listed on this answer: File comparator utilities
Upvotes: 1
Reputation: 140457
One simple solution: don't pass in a file object. But instead a factory that creates an OutputStream for you.
At runtime, this could be a FileOutputStream. But for testing, you could pass a different factory that creates, say a ByteArrayOutputStream. Then your code writes to memory without knowing it.
And then you could write another test that reads back these bytes.
Upvotes: 1