/img/java-banner.png

Testing Print Output in JUnit

Re-routing standard out from the console to a testable output stream.

Written by: Alex Root-Roatch | Sunday, August 11, 2024

Creating an Output Stream

In order to re-route printing from going to the console, we need something else to send that data to and store it for access later. This can be done with a ByteArrayOutputStream:

ByteArrayOutputStream outContent = new ByteArrayOutputStream();

Saving the Current Output Setting

At the end of the tests, we'll want to set standard out back to the console to restore expected functionality. Let's save the current output configuration to a variable to access later before redefining standard out:

final PrintStream originalOut = System.out;

Setting Standard Out to the Output Stream

Now we re-route standard out from the console to the outContent output stream we created. We need to create a PrintStream that prints to outContent and send standard out to that:

System.setOut(new PrintStream(outContent));

Testing

The result of System.out.print will now be stored to outContent as an array of bytes. To test that the content is what it should be, it needs to be converted to a string first:

assertTrue(outContent.toString().contains("Hello!"));

Reset Standard Out

After the tests run, it's important to restore expected functionality by setting standard out back to the console. This is where the originalOut variable comes in:

System.setOut(originalOut);

Explore more articles

Browse All Posts