Class CapturedOutput
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:
- String access methods:
getOut()
,getErr()
,getAll()
- Line access methods:
getOutLines()
,getErrLines()
,getAllLines()
- Byte array access methods:
getOutAsBytes()
,getErrAsBytes()
- Chronological access methods:
getChronologicalEntries()
,getChronologicalContent()
- Utility methods:
isEmpty()
,contains(String)
,outContains(String)
,errContains(String)
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Represents a single output entry with type, content, and timestamp.static enum
Enumeration of output types for distinguishing between stdout and stderr. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Searches for the specified text within both stdout and stderr content.boolean
errContains
(String text) Searches for the specified text within the captured stderr content.getAll()
Combines and retrieves both stdout and stderr content as a single string.Retrieves the combined stdout and stderr content as a list of lines.Retrieves all output content in chronological order as a single string.Retrieves all output entries in chronological order.Retrieves all output content in chronological order as a list of lines.getErr()
Retrieves the captured stderr content as a string.byte[]
Retrieves the captured stderr content as a raw byte array.Retrieves the captured stderr content as a list of lines.getOut()
Retrieves the captured stdout content as a string.byte[]
Retrieves the captured stdout content as a raw byte array.Retrieves the captured stdout content as a list of lines.boolean
isEmpty()
Determines if any output was captured to either stdout or stderr.boolean
outContains
(String text) Searches for the specified text within the captured stdout content.toString()
Provides a string representation of this captured output for debugging purposes.
-
Method Details
-
contains
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 benull
)- Returns:
true
if either stdout or stderr contains the specified text,false
otherwise- Throws:
NullPointerException
- if text isnull
- See Also:
-
errContains
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 benull
)- Returns:
true
if stderr contains the specified text,false
otherwise- Throws:
NullPointerException
- if text isnull
- See Also:
-
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
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
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
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
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
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. -
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
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
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. -
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
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. -
outContains
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 benull
)- Returns:
true
if stdout contains the specified text,false
otherwise- Throws:
NullPointerException
- if text isnull
- See Also:
-
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'}
-