Class GenericExpectedLogging<APPENDER extends LogEventCaptureAppender>

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

    public abstract class GenericExpectedLogging<APPENDER extends LogEventCaptureAppender>
    extends java.lang.Object
    implements org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback
    The ExpectedLogging rule allows you to verify that your code logs messages in the underlying logging framework.

    Usage

     public class SimpleLoggingTest {
         @Rule
         public ExpectedLogging logging = ExpectedLogging.forSource(LoggingSource.class);
    
         @Test
         public void shouldLogError() {
              String message = "Error Message";
    
              new LoggingSource().doSomethingThatLogsAnError();
    
              LoggingAssertions.assertThat(logging).hasErrorMessage(message);
         }
     }

    You have to add the ExpectedLogging rule to your test. This doesn't affect your existing tests. After executing the method that is expected to log, you can verify this with assertj assertions defined in ExpectedLoggingAssert

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void afterEach​(org.junit.jupiter.api.extension.ExtensionContext context)  
      void beforeEach​(org.junit.jupiter.api.extension.ExtensionContext context)  
      java.util.List<LogEvent> getLogEvents()  
      • Methods inherited from class java.lang.Object

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

      • beforeEach

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

        public void afterEach​(org.junit.jupiter.api.extension.ExtensionContext context)
        Specified by:
        afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback
      • getLogEvents

        public java.util.List<LogEvent> getLogEvents()