Class CapturedOutput

java.lang.Object
rife.bld.extension.testing.CapturedOutput

public class CapturedOutput extends Object
Container for captured stdout and stderr output during test execution.

This class provides methods to analyze and inspect console output that was captured during a test method annotated with CaptureOutput. All captured output is converted using the default character encoding.

Instances of this class are automatically created by the CaptureOutputExtension and injected into test methods as parameters. The class offers various methods to access and analyze the captured output:

Usage Example:

 @Test
 @CaptureOutput
 void testConsoleOutput(CapturedOutput output) {
     System.out.println("Hello World");
     System.err.println("Error");

     // Access individual streams
     assertEquals("Hello World\n", output.getOut());
     assertEquals("Error\n", output.getErr());

     // Access chronological output
     List<OutputEntry> entries = output.getChronologicalEntries();
     assertEquals(OutputType.STDOUT, entries.get(0).getType());
     assertEquals("Hello World\n", entries.get(0).getContent());

     // Get chronological content as single string
     String chronological = output.getChronologicalContent();

     // Search within streams
     assertTrue(output.outContains("Hello"));
     assertTrue(output.errContains("Error"));
     assertTrue(output.contains("World"));

     // Check if output is empty
     assertFalse(output.isEmpty());
 }

Thread Safety: This class is not thread-safe. Each instance should only be used within the context of a single test method execution.

Since:
1.0
Author:
Erik C. Thauvin
See Also:
  • Method Details

    • contains

      public boolean contains(String text)
      Searches for the specified text within both stdout and stderr content.

      This method performs a case-sensitive substring search within both the stdout and stderr content. It returns true if the text is found in either stream. This is useful when you don't care which specific stream contains the text, just that it was output somewhere.

      This method is equivalent to:
      outContains(text) || errContains(text)

      Parameters:
      text - the text to search for in either stdout or stderr (must not be null)
      Returns:
      true if either stdout or stderr contains the specified text, false otherwise
      Throws:
      NullPointerException - if text is null
      See Also:
    • errContains

      public boolean errContains(String text)
      Searches for the specified text within the captured stderr content.

      This method performs a case-sensitive substring search within the stderr content. It's equivalent to calling getErr().contains(text) but provides a more readable API for common assertions.

      Parameters:
      text - the text to search for in stderr (must not be null)
      Returns:
      true if stderr contains the specified text, false otherwise
      Throws:
      NullPointerException - if text is null
      See Also:
    • getAll

      public String getAll()
      Combines and retrieves both stdout and stderr content as a single string.

      This method concatenates the stdout content followed by the stderr content. The order reflects the sequence in which output was written during test execution, with stdout content appearing first, followed by stderr content.

      Note: This does not preserve the exact interleaving of stdout and stderr as it would appear in a real console, but rather groups all stdout first, then all stderr. For chronological ordering, use getChronologicalContent().

      Returns:
      the combined stdout and stderr content, or empty string if no output was captured
      See Also:
    • getAllLines

      public List<String> getAllLines()
      Retrieves the combined stdout and stderr content as a list of lines.

      This method splits the combined output (stdout followed by stderr) into individual lines using Java's built-in line processing. Empty lines are preserved in the result. The line splitting handles various line separator formats (\n, \r\n, \r) correctly across different platforms.

      Note: For chronological line ordering, use getChronologicalLines().

      Returns:
      a list of lines from the combined output, or empty list if no output was captured
      See Also:
    • getChronologicalContent

      public String getChronologicalContent()
      Retrieves all output content in chronological order as a single string.

      This method combines all captured output (both stdout and stderr) in the exact order it was written during test execution. This preserves the interleaving of stdout and stderr as it would appear in a real console.

      Returns:
      the chronologically ordered output content, or empty string if no output was captured
      See Also:
    • getChronologicalEntries

      public List<CapturedOutput.OutputEntry> getChronologicalEntries()
      Retrieves all output entries in chronological order.

      This method returns an immutable list of all output entries, preserving the exact order in which stdout and stderr output occurred during test execution. Each entry includes the output type, content, and timestamp.

      Returns:
      an immutable list of output entries in chronological order
      See Also:
    • getChronologicalLines

      public List<String> getChronologicalLines()
      Retrieves all output content in chronological order as a list of lines.

      This method splits the chronologically ordered output into individual lines using Java's built-in line processing. Empty lines are preserved in the result. The line splitting handles various line separator formats (\n, \r\n, \r) correctly across different platforms.

      Returns:
      a list of lines from the chronologically ordered output, or an empty list if no output was captured
      See Also:
    • getErr

      public String getErr()
      Retrieves the captured stderr content as a string.

      This method converts all data written to System.err during the test execution into a string using the default character encoding. Line separators are preserved as they were originally written.

      Returns:
      the complete stderr content as a string, or empty string if no stderr was captured
      See Also:
    • getErrAsBytes

      public byte[] getErrAsBytes()
      Retrieves the captured stderr content as a raw byte array.

      This method provides access to the raw bytes that were written to stderr, without any character encoding conversion. This can be useful for analyzing binary data or when specific encoding handling is required.

      Returns:
      a copy of the stderr byte data, or empty array if no stderr was captured
      See Also:
    • getErrLines

      public List<String> getErrLines()
      Retrieves the captured stderr content as a list of lines.

      This method splits the stderr output into individual lines using Java's built-in line processing. Empty lines are preserved in the result. The line splitting handles various line separator formats (\n, \r\n, \r) correctly across different platforms.

      Returns:
      a list of lines from stderr, or empty list if no stderr was captured
      See Also:
    • getOut

      public String getOut()
      Retrieves the captured stdout content as a string.

      This method converts all data written to System.out during the test execution into a string using the default character encoding. Line separators are preserved as they were originally written.

      Returns:
      the complete stdout content as a string, or empty string if no stdout was captured
      See Also:
    • getOutAsBytes

      public byte[] getOutAsBytes()
      Retrieves the captured stdout content as a raw byte array.

      This method provides access to the raw bytes that were written to stdout, without any character encoding conversion. This can be useful for analyzing binary data or when specific encoding handling is required.

      Returns:
      a copy of the stdout byte data, or empty array if no stdout was captured
      See Also:
    • getOutLines

      public List<String> getOutLines()
      Retrieves the captured stdout content as a list of lines.

      This method splits the stdout output into individual lines using Java's built-in line processing. Empty lines are preserved in the result. The line splitting handles various line separator formats (\n, \r\n, \r) correctly across different platforms.

      Returns:
      a list of lines from stdout, or empty list if no stdout was captured
      See Also:
    • isEmpty

      public boolean isEmpty()
      Determines if any output was captured to either stdout or stderr.

      This method returns true only when both stdout and stderr are completely empty (zero bytes captured). It's useful for verifying that a test method produces no console output.

      Returns:
      true if both stdout and stderr are empty, false otherwise
      See Also:
    • outContains

      public boolean outContains(String text)
      Searches for the specified text within the captured stdout content.

      This method performs a case-sensitive substring search within the stdout content. It's equivalent to calling getOut().contains(text) but provides a more readable API for common assertions.

      Parameters:
      text - the text to search for in stdout (must not be null)
      Returns:
      true if stdout contains the specified text, false otherwise
      Throws:
      NullPointerException - if text is null
      See Also:
    • toString

      public String toString()
      Provides a string representation of this captured output for debugging purposes.

      The returned string includes both the stdout and stderr content in a structured format that's useful for debugging test failures. The format shows the class name and both captured streams.

      Example output:
      CapturedOutput{stdout='Hello World\n', stderr='Error\n'}

      Overrides:
      toString in class Object
      Returns:
      a string representation of the captured output