Class CaptureOutputExtension
- All Implemented Interfaces:
AfterEachCallback
,BeforeEachCallback
,Extension
,ParameterResolver
,TestInstantiationAwareExtension
This extension is automatically applied when using the CaptureOutput
annotation. It intercepts the standard output and error streams before test
execution and restores them afterward, making the captured content available
through parameter injection with chronological tracking support.
The extension implements the following JUnit 5 extension interfaces:
BeforeEachCallback
- Sets up output capture before each testAfterEachCallback
- Restores original streams after each testParameterResolver
- InjectsCapturedOutput
parameters
The extension supports chronological tracking of output, recording both the type (stdout/stderr) and timestamp of each output operation.
Note: This class is not intended to be used directly.
Use the CaptureOutput
annotation instead.
- Since:
- 1.0
- Author:
- Erik C. Thauvin
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension
TestInstantiationAwareExtension.ExtensionContextScope
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
afterEach
(ExtensionContext context) Restores original output streams after each test method execution.void
beforeEach
(ExtensionContext context) Sets up output capture before each test method execution.resolveParameter
(ParameterContext parameterContext, ExtensionContext extensionContext) Resolves theCapturedOutput
parameter for injection into test methods.boolean
supportsParameter
(ParameterContext parameterContext, ExtensionContext extensionContext) Determines if this extension can resolve the given parameter.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension
getTestInstantiationExtensionContextScope
-
Constructor Details
-
CaptureOutputExtension
public CaptureOutputExtension()
-
-
Method Details
-
afterEach
Restores original output streams after each test method execution.This method restores the original stdout and stderr streams and closes the capture streams to free up resources. This ensures that later tests and framework output work normally.
- Specified by:
afterEach
in interfaceAfterEachCallback
- Parameters:
context
- the current extension context- Throws:
Exception
- if an error occurs during cleanup
-
beforeEach
Sets up output capture before each test method execution.This method stores references to the original stdout and stderr streams, creates new capture streams with chronological tracking, and redirects system output to the capture streams. The captured output is stored in the extension context for later parameter injection.
- Specified by:
beforeEach
in interfaceBeforeEachCallback
- Parameters:
context
- the current extension context
-
supportsParameter
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) Determines if this extension can resolve the given parameter.This method returns
true
only for parameters of typeCapturedOutput
, enabling automatic injection of the captured output data into test methods.- Specified by:
supportsParameter
in interfaceParameterResolver
- Parameters:
parameterContext
- the context for the parameter for which resolution is attemptedextensionContext
- the extension context for the executable about to be invoked- Returns:
true
if the parameter type isCapturedOutput
-
resolveParameter
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) Resolves theCapturedOutput
parameter for injection into test methods.This method retrieves the captured output instance that was stored during the
beforeEach(ExtensionContext)
callback and returns it for injection into the test method parameter.- Specified by:
resolveParameter
in interfaceParameterResolver
- Parameters:
parameterContext
- the context for the parameter for which resolution is attemptedextensionContext
- the extension context for the executable about to be invoked- Returns:
- the
CapturedOutput
instance containing captured stdout and stderr
-