Class LogTesterJUnit5

  • All Implemented Interfaces:
    org.junit.jupiter.api.extension.AfterTestExecutionCallback, org.junit.jupiter.api.extension.BeforeTestExecutionCallback, org.junit.jupiter.api.extension.Extension

    public class LogTesterJUnit5
    extends java.lang.Object
    implements org.junit.jupiter.api.extension.BeforeTestExecutionCallback, org.junit.jupiter.api.extension.AfterTestExecutionCallback
    For tests only
    This JUnit 5 extension 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");
       }
     }
    
     class MyClassTests {
       @org.junit.jupiter.api.extension.RegisterExtension
       LogTesterJUnit5 logTester = new LogTesterJUnit5();
    
       @org.junit.jupiter.api.Test
       public void test_log() {
         new MyClass().doSomething();
    
         assertThat(logTester.logs()).containsOnly("foo");
       }
     }
     
    Since:
    8.1
    • Constructor Summary

      Constructors 
      Constructor Description
      LogTesterJUnit5()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void after()  
      void afterTestExecution​(org.junit.jupiter.api.extension.ExtensionContext context)  
      protected void before()  
      void beforeTestExecution​(org.junit.jupiter.api.extension.ExtensionContext context)  
      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

      • LogTesterJUnit5

        public LogTesterJUnit5()
    • Method Detail

      • beforeTestExecution

        public void beforeTestExecution​(org.junit.jupiter.api.extension.ExtensionContext context)
                                 throws java.lang.Exception
        Specified by:
        beforeTestExecution in interface org.junit.jupiter.api.extension.BeforeTestExecutionCallback
        Throws:
        java.lang.Exception
      • afterTestExecution

        public void afterTestExecution​(org.junit.jupiter.api.extension.ExtensionContext context)
                                throws java.lang.Exception
        Specified by:
        afterTestExecution in interface org.junit.jupiter.api.extension.AfterTestExecutionCallback
        Throws:
        java.lang.Exception
      • 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()