Class LogRule

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

    @Deprecated
    public class LogRule
    extends Object
    implements org.junit.rules.TestRule
    Deprecated.
    starting with 7.0M1 you should use AllLogRule instead since we want tests to not output anything to the console
    Allow capturing Logs output in the Class under test during unit testing. 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 LogRule logRule = new LogRule() {{
          record(LogLevel.WARN);
          recordLoggingForType(RestrictParseLocationEventHandler.class);
      }};
     
    Since:
    4.2RC1
    Version:
    $Id: 43bf6efed045d59d86f7ca694f2cca1de03c3a1f $
    • Constructor Detail

      • LogRule

        public LogRule()
        Deprecated.
    • Method Detail

      • apply

        public org.junit.runners.model.Statement apply​(org.junit.runners.model.Statement statement,
                                                       org.junit.runner.Description description)
        Deprecated.
        Specified by:
        apply in interface org.junit.rules.TestRule
      • record

        public void record​(LogRule.LogLevel level)
        Deprecated.
        Parameters:
        level - the log level to capture
      • recordLoggingForType

        public void recordLoggingForType​(Class<?> type)
        Deprecated.
        Parameters:
        type - the logging class type for which to capture logs
      • contains

        public boolean contains​(String logOutput)
        Deprecated.
        Parameters:
        logOutput - the log output to match in the captured data
        Returns:
        true if the passed text was logged or false otherwise
      • getMessage

        public String getMessage​(int position)
        Deprecated.
        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)
        Deprecated.
        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()
        Deprecated.
        Returns:
        the number of log messages that have been captured