Java, Junit – Capture the standard input / Output for use in a unit test [duplicate]

Use System.setOut() (and System.setErr()) to redirect the output to an arbitrary printstream – which can be one that you read from programmatically.

For example:

final ByteArrayOutputStream myOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(myOut));

// test stuff here...

final String standardOutput = myOut.toString();

Leave a Comment

techhipbettruvabetnorabahisbahis forumuedueduedusedueduedusedueduseduedus