If not already done so, rewrite the parts of your application to take the output as a parameter, instead of writing directly to System.out
. This is usually better design regardless of testing.
In each test, create a new output stream (or spy on System.out
if you must) and pass it to the code unit under test.