Class CaptureOutputExtension

java.lang.Object
rife.bld.extension.testing.CaptureOutputExtension
All Implemented Interfaces:
AfterEachCallback, BeforeEachCallback, Extension, ParameterResolver, TestInstantiationAwareExtension

public class CaptureOutputExtension extends Object implements BeforeEachCallback, AfterEachCallback, ParameterResolver
JUnit extension that captures stdout and stderr during test execution.

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:

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:
  • Constructor Details

    • CaptureOutputExtension

      public CaptureOutputExtension()
  • Method Details

    • afterEach

      public void afterEach(ExtensionContext context) throws Exception
      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 interface AfterEachCallback
      Parameters:
      context - the current extension context
      Throws:
      Exception - if an error occurs during cleanup
    • beforeEach

      public void beforeEach(ExtensionContext context)
      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 interface BeforeEachCallback
      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 type CapturedOutput, enabling automatic injection of the captured output data into test methods.

      Specified by:
      supportsParameter in interface ParameterResolver
      Parameters:
      parameterContext - the context for the parameter for which resolution is attempted
      extensionContext - the extension context for the executable about to be invoked
      Returns:
      true if the parameter type is CapturedOutput
    • resolveParameter

      public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
      Resolves the CapturedOutput 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 interface ParameterResolver
      Parameters:
      parameterContext - the context for the parameter for which resolution is attempted
      extensionContext - the extension context for the executable about to be invoked
      Returns:
      the CapturedOutput instance containing captured stdout and stderr