Class LogCaptureExtension

  • All Implemented Interfaces:
    org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension

    public class LogCaptureExtension
    extends Object
    implements org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback
    Captures any logging done by code being tested in order for the logging to not leak in the console and to be able to assert the content of the logging statements.

    Usage:

    • Must be used with @RegisterExtension and using a non private variable
    Example:
     
     @RegisterExtension
     LogCaptureExtension logCapture = new LogCaptureExtension(LogLevel.INFO);
     
     
    Since:
    10.4RC1
    Version:
    $Id: 64a02e1f7c8bcf9fb7f72ca23385cef421d3660c $
    • Constructor Detail

      • LogCaptureExtension

        public LogCaptureExtension​(LogLevel level)
        Parameters:
        level - the logging level from which to start capturing logs (for example if LogLevel.INFO then INFO, WARN, ERROR, etc are captured too).
      • LogCaptureExtension

        public LogCaptureExtension()
        Captures all logs from severity LogLevel.INFO and higher (WARN, ERROR, etc).
    • Method Detail

      • beforeAll

        public void beforeAll​(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
        Specified by:
        beforeAll in interface org.junit.jupiter.api.extension.BeforeAllCallback
      • afterAll

        public void afterAll​(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
                      throws Exception
        Specified by:
        afterAll in interface org.junit.jupiter.api.extension.AfterAllCallback
        Throws:
        Exception
      • beforeEach

        public void beforeEach​(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
        Specified by:
        beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
      • afterEach

        public void afterEach​(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
                       throws Exception
        Specified by:
        afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback
        Throws:
        Exception
      • getLogEvent

        public ch.qos.logback.classic.spi.ILoggingEvent getLogEvent​(int position)
        Parameters:
        position - the message number in the list of captured logs
        Returns:
        the logging event corresponding to the message, allowing to get information such as the level, the marker, the formatted string, etc
      • getMessage

        public String getMessage​(int position)
        Parameters:
        position - the message number in the list of captured logs
        Returns:
        the message at the specified position
      • getMarker

        public org.slf4j.Marker getMarker​(int position)
        Parameters:
        position - the message number in the list of captured logs
        Returns:
        the marker at the specified position
      • size

        public int size()
        Returns:
        the number of log messages that have been captured
      • ignoreAllMessages

        public void ignoreAllMessages()
        Voluntarily ignore all messages to signify they should not need to be asserted.
      • ignoreMessage

        public void ignoreMessage​(int position)
        Voluntarily ignore a message to signify it should not need to be asserted.
        Parameters:
        position - the message number in the list of captured logs