TestNG - Unit test standard output, System.out.println

By xngo on March 9, 2019

In order to unit test the standard output, we need to redirect what are being print to a new output stream. Then, do the checking on the new output stream. This can be accomplished by using System.setOut() method to swap between the new and the original output stream. Before every test method, you set the standard output to use the new output stream. After you are done testing on the method, you set the standard output to use the original output.

Here is the code showing how to do it using TestNG.

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Random;
 
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
 
import static org.testng.Assert.assertTrue;
 
public class StandardOutputTest {
    // Variables
    Random random = new Random();
    final int MAX = 100; // Milliseconds.
 
    // Store the original standard out before changing it.
    private final PrintStream originalStdOut = System.out;
    private ByteArrayOutputStream consoleContent = new ByteArrayOutputStream();
 
    @BeforeMethod
    public void beforeTest() {
        // Redirect all System.out to consoleContent.
        System.setOut(new PrintStream(this.consoleContent));
    }
 
    @Test(description = "Test method 1", invocationCount = 3)
    public void testMethod1() {
        final int iRnd = this.random.nextInt(MAX * 10);
        this.pause(iRnd);
        System.out.print(String.format("Test method 1 (Work Time = %d ms).", iRnd));
 
        assertTrue(this.consoleContent.toString().indexOf("Test method 1") != -1);
    }
 
    @Test(description = "Test method 2", invocationCount = 3)
    public void testMethod2() {
        final int iRnd = this.random.nextInt(MAX);
        this.pause(iRnd);
        System.out.print(String.format("Test method 2 (Work Time = %d ms).", iRnd));
 
        assertTrue(this.consoleContent.toString().indexOf("Test method 2") != -1);
    }
 
    @AfterMethod
    public void afterTest() {
        // Put back the standard out.
        System.setOut(this.originalStdOut);
 
        // Print what has been captured.
        System.out.println(this.consoleContent.toString());
        System.out.println(String.format("              ====>Captured Console length=%d",
                this.consoleContent.toString().length()));
 
        // Clear the consoleContent.
        this.consoleContent = new ByteArrayOutputStream();
    }
 
    /***********
     * Helpers
     ************/
    // Simulate some processing time by pausing.
    private void pause(long lPauseInMillisSec) {
        try {
            Thread.sleep(lPauseInMillisSec);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
 
    }
}

Output

Test method 1 (Work Time = 293 ms).
              ====>Captured Console length=35
Test method 1 (Work Time = 585 ms).
              ====>Captured Console length=35
Test method 1 (Work Time = 867 ms).
              ====>Captured Console length=35
Test method 2 (Work Time = 67 ms).
              ====>Captured Console length=34
Test method 2 (Work Time = 11 ms).
              ====>Captured Console length=34
Test method 2 (Work Time = 57 ms).
              ====>Captured Console length=34

Github

  • https://github.com/xuanngo2001/java-testng/blob/master/src/net/openwritings/testng/StandardOutputTest.java

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.