Class AllLogRule

  • All Implemented Interfaces:
    org.junit.rules.TestRule

    public class AllLogRule
    extends Object
    implements org.junit.rules.TestRule
    Allow capturing Logs output during the execution of the unit test. This is useful for two reasons:
    • it allows not outputting log messages in the console which is a bad practice. When a test run it should not output anything and if it needs to assert something, it has to be done in the test itself.
    • it allows to assert the output log messages
    This code was inspired by code written by Christian Baranowski in a blog post.

    Example usage:

    
      @Rule public AllLogRule logRule = new AllLogRule();
     
    Since:
    7.0M1
    Version:
    $Id: 99cdd91537b4da77543a58f7fc7b4b222ad2c9cc $
    • Constructor Detail

      • AllLogRule

        public AllLogRule()
        Capture INFO log.
      • AllLogRule

        public AllLogRule​(LogLevel level)
        Caputure passed log level.
        Parameters:
        level - the level of log to capture
    • Method Detail

      • apply

        public org.junit.runners.model.Statement apply​(org.junit.runners.model.Statement statement,
                                                       org.junit.runner.Description description)
        Specified by:
        apply in interface org.junit.rules.TestRule
      • 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
        Since:
        10.4RC1
      • 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
        Since:
        7.0M2
      • 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.
        Since:
        10.4RC1
      • 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
        Since:
        10.4RC1