Class LogTester

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

    public class LogTester
    extends java.lang.Object
    implements org.junit.rules.TestRule
    For tests only
    This JUnit rule allows to configure and access logs in tests. By default trace level is enabled.
    Warning - not compatible with parallel execution of tests in the same JVM fork.
    Example:
     public class MyClass {
       private final Logger logger = Loggers.get("logger_name");
    
       public void doSomething() {
         logger.info("foo");
       }
     }
    
     public class MyTest {
       @org.junit.Rule
       public LogTester logTester = new LogTester();
    
       @org.junit.Test
       public void test_log() {
         new MyClass().doSomething();
    
         assertThat(logTester.logs()).containsOnly("foo");
       }
     }
     
    Since:
    5.1
    • Constructor Summary

      Constructors 
      Constructor Description
      LogTester()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void after()  
      org.junit.runners.model.Statement apply​(org.junit.runners.model.Statement base, org.junit.runner.Description description)  
      protected void before()  
      G clear()  
      java.util.List<LogAndArguments> getLogs()
      Logs with arguments in chronological order (item at index 0 is the oldest one)
      java.util.List<LogAndArguments> getLogs​(LoggerLevel level)
      Logs with arguments in chronological order (item at index 0 is the oldest one) for a given level
      java.util.List<java.lang.String> logs()
      Logs in chronological order (item at index 0 is the oldest one)
      java.util.List<java.lang.String> logs​(LoggerLevel level)
      Logs in chronological order (item at index 0 is the oldest one) for a given level
      G setLevel​(LoggerLevel level)
      Enable/disable debug logs.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • LogTester

        public LogTester()
    • Method Detail

      • apply

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

        protected void before()
      • after

        protected void after()
      • setLevel

        public G setLevel​(LoggerLevel level)
        Enable/disable debug logs. Info, warn and error logs are always enabled. By default INFO logs are enabled when LogTester is started.
      • logs

        public java.util.List<java.lang.String> logs()
        Logs in chronological order (item at index 0 is the oldest one)
      • logs

        public java.util.List<java.lang.String> logs​(LoggerLevel level)
        Logs in chronological order (item at index 0 is the oldest one) for a given level
      • getLogs

        public java.util.List<LogAndArguments> getLogs()
        Logs with arguments in chronological order (item at index 0 is the oldest one)
      • getLogs

        public java.util.List<LogAndArguments> getLogs​(LoggerLevel level)
        Logs with arguments in chronological order (item at index 0 is the oldest one) for a given level
      • clear

        public G clear()