Skip to content

A JUnit5 extension to assist monitoring Logback log events

License

Notifications You must be signed in to change notification settings

MarceloLeite2604/spy-root-logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spy Root Logger

A JUnit5 extension to assist monitoring Logback log events and elaborate tests to confirm that events were logged.

Examples

  import com.figtreelake.spyrootlogger.SpyRootLogger;
  import com.figtreelake.spyrootlogger.SpyRootLoggerExtension;
  import com.figtreelake.spyrootlogger.SpyRootLoggerInject;
  import org.junit.jupiter.api.BeforeEach;
  import org.junit.jupiter.api.extension.ExtendWith;
      
  import java.util.regex.Pattern;
         
  @ExtendWith(SpyRootLoggerExtension.class)
  class FooTest {
         
    private Foo foo;
         
    @SpyRootLoggerInject
    private SpyRootLogger spyRootLogger;
         
    @BeforeEach
    void setUp() {
      foo = new Foo();
    }
         
    @Test
    void shouldLogExceptionWhenHandlingRuntimeException() {
      final var exception = new RuntimeException("Dummy exception");
   
      final var responseEntity = foo.handleException(exception);
         
      /* Spy Root Logger can be used to check if there is a log event with 
      a specific exception attached. */
      assertThat(spyRootLogger.findEventsByThrowableAttached(exception))
          .isNotEmpty();
    }
         
    @Test
    void shouldLogErrorEventWhenHandlingIllegalStateException() {
      final var mockedIllegalStateException = mock(IllegalStateException.class);
   
      final var responseEntity = foo.handleException(mockedIllegalStateException);
   
      /* Spy Root Logger can be used to check how many events of such level were 
      logged */
      assertThat(spyRootLogger.countErrorEvents()).isPositive();
    }
         
    @Test
    void shouldLogMessageInformingMessageNotReadableWhenHandlingHttpMessageNotReadableException() {
      final var mockedHttpMessageNotReadableException = mock(HttpMessageNotReadableException.class);
         
      final var messagePattern = Pattern.compile("^Failed to read HTTP message");
         
      final var responseEntity = foo.handleException(mockedHttpMessageNotReadableException);
         
      /* Spy Root Logger can be used to find log events by regular expressions 
      on its message */
      assertThat(spyRootLogger.findEventsByMessagePattern(messagePattern))
        .isNotEmpty();
    }
  }

Note: Functional examples can be found on Spy Root Logger Example repository.

Usage

  1. Be sure to implement your tests using JUnit5 framework.

  2. Add Spy Root Logger on your project.

    1. For Maven projects add the following on your pom.xml file under <dependencies> tag.

      <dependency>
        <groupId>com.figtreelake</groupId>
        <artifactId>spy-root-logger</artifactId>
        <version>1.0</version>
      </dependency>
    2. For Gradle projects add the following on your build.gradle file under dependencies declaration.

      implementation 'com.figtreelake:spy-root-logger:1.0'
    3. You can check the latest version available on Maven Central repository.

  3. Annotate your test class with @ExtendWith informing SpyRootLoggerExtension class as value.

  4. Add a SpyRootLogger field and annotate it with @SpyRootLoggerInject.

       import com.figtreelake.spyrootlogger.SpyRootLogger;
       import com.figtreelake.spyrootlogger.SpyRootLoggerExtension;
       import com.figtreelake.spyrootlogger.SpyRootLoggerInject;
       import org.junit.jupiter.api.extension.ExtendWith;
    
       @ExtendWith(SpyRootLoggerExtension.class)
       class FooTest {
    
         @SpyRootLoggerInject
         private SpyRootLogger spyRootLogger;
    
         /* Test declarations... */
       }

Contributions

Try out the library. If you like the outcome, give a star for its repository, share or talk about it with your IT friends and colleagues. This is a work I have been doing in my spare time and I really would like to see that people appreciate the time I have invested on it.

If you liked the project and really want to demonstrate your appreciation, you can send me a "thank you" coffee. 🙂

Yellow PayPal Donation button with "donate" text written on it

About

A JUnit5 extension to assist monitoring Logback log events

Topics

Resources

License

Stars

Watchers

Forks

Languages