To see any output with the PrintWriter
you need to close()
or flush()
it.
Alternatively you can create the PrintWriter
with the autoFlush
parameter, e.g.:
final FileOutputStream fos = new FileOutputStream("somefile.txt");
final PrintWriter writer = new PrintWriter(fos, true); // <-- autoFlush
This will write to the file when println
, printf
or format
is invoked.
I would say closing the PrintWriter
is preferable.
Aside:
Have you considered mocking the Writer
? You could avoid writing to a file and verify the expected calls instead, e.g.:
verify(writer).println("xyz");