Class SystemOutRule

java.lang.Object
org.junit.contrib.java.lang.system.SystemOutRule
All Implemented Interfaces:
org.junit.rules.TestRule

public class SystemOutRule extends Object implements org.junit.rules.TestRule
The SystemOutRule intercepts the writes to System.out. It is used to make assertions about the text that is written to System.out or to mute System.out.

Assertions

SystemOutRule may be used for verifying the text that is written to System.out.

 public class SystemOutTest {
   @Rule
   public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();

   @Test
   public void test() {
     System.out.print("some text");
     assertEquals("some text", systemOutRule.getLog());
   }
 }
 

If your code under test writes the correct new line characters to System.out then the test output is different at different systems. getLogWithNormalizedLineSeparator() provides a log that always uses \n as line separator. This makes it easy to write appropriate assertions that work on all systems.

 public class SystemOutTest {
   @Rule
   public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();

   @Test
   public void test() {
     System.out.print(String.format("some text%n"));
     assertEquals("some text\n", systemOutRule.getLogWithNormalizedLineSeparator());
   }
 }
 

If your code under test writes raw binary data to System.out then you can read it by means of getLogAsBytes()).

 public class SystemOutTest {
   @Rule
   public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();

   @Test
   public void test() {
     byte[] data = { 1, 2, 3, 4, 5 };
     System.out.write(data, 0, data.length);
     assertEquals(data, systemOutRule.getLogAsBytes());
   }
 }
 

You don't have to enable logging for every test. It can be enabled for specific tests only.

 public class SystemOutTest {
   @Rule
   public final SystemOutRule systemOutRule = new SystemOutRule();

   @Test
   public void testWithLogging() {
     systemOutRule.enableLog()
     System.out.print("some text");
     assertEquals("some text", systemOutRule.getLog());
   }

   @Test
   public void testWithoutLogging() {
     System.out.print("some text");
   }
 }
 

If you want to verify parts of the output only then you can clear the log during a test.

 public class SystemOutTest {
   @Rule
   public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();

   @Test
   public void test() {
     System.out.print("uninteresting things");
     systemOutRule.clearLog()
     System.out.print("interesting things");
     assertEquals("interesting things", systemOutRule.getLog());
   }
 }
 

Muting

Usually the output of a test to System.out does not have to be visible. It may even slowdown the test. SystemOutRule can be used to suppress this output.

 public class SystemOutTest {
   @Rule
   public final SystemOutRule systemOutRule = new SystemOutRule().mute();

   @Test
   public void test() {
     System.out.print("some text"); //is not visible on the console
   }
 }
 

You don't have to mute System.out for every test. It can be muted for specific tests only.

 public class SystemOutTest {
   @Rule
   public final SystemOutRule systemOutRule = new SystemOutRule();

   @Test
   public void testWithSuppressedOutput() {
     systemOutRule.mute()
     System.out.print("some text");
   }

   @Test
   public void testWithNormalOutput() {
     System.out.print("some text");
   }
 }
 

In case of a failed test it is sometimes helpful to see the output. This is when the method muteForSuccessfulTests() comes into play.

 public class SystemOutTest {
   @Rule
   public final SystemOutRule systemOutRule = new SystemOutRule().muteForSuccessfulTests();

   @Test
   public void testWithSuppressedOutput() {
     System.out.print("some text");
   }

   @Test
   public void testWithNormalOutput() {
     System.out.print("some text");
     fail();
   }
 }
 

Combine Logging and Muting

Logging and muting can be combined. No output is actually written to System.out but everything that would have been written is available from the log.

 public class SystemOutTest {
   @Rule
   public final SystemOutRule systemOutRule = new SystemOutRule().mute().enableLog();

   @Test
   public void test() {
     System.out.print("some text"); //is not visible on the console
     assertEquals("some text", systemOutRule.getLog()); //succeeds
   }
 }
 
  • Field Details

  • Constructor Details

    • SystemOutRule

      public SystemOutRule()
  • Method Details

    • mute

      public SystemOutRule mute()
      Suppress the output to System.out.
      Returns:
      the rule itself.
    • muteForSuccessfulTests

      public SystemOutRule muteForSuccessfulTests()
      Suppress the output to System.out for successful tests only. The output is still written to System.out for failing tests.
      Returns:
      the rule itself.
    • clearLog

      public void clearLog()
      Clears the current log.
    • getLog

      public String getLog()
      Returns the text that is written to System.out since enableLog() (respectively clearLog() has been called.
      Returns:
      the text that is written to System.out since enableLog() (respectively clearLog() has been called.
    • getLogWithNormalizedLineSeparator

      public String getLogWithNormalizedLineSeparator()
      Returns the text that is written to System.out since enableLog() (respectively clearLog() has been called. New line characters are replaced with a single \n.
      Returns:
      the normalized log.
    • getLogAsBytes

      public byte[] getLogAsBytes()
      Returns the raw bytes that are written to System.out since enableLog() (respectively clearLog() has been called.
      Returns:
      the raw bytes that are written to System.out since enableLog() (respectively clearLog() has been called.
    • enableLog

      public SystemOutRule enableLog()
      Start logging of everything that is written to System.out.
      Returns:
      the rule itself.
    • apply

      public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description)
      Specified by:
      apply in interface org.junit.rules.TestRule