Class LoggingTestKit


  • public abstract class LoggingTestKit
    extends java.lang.Object
    Facilities for verifying logs.

    Requires Logback.

    See the static factory methods as starting point for creating LoggingTestKit.

    Not for user extension.

    • Constructor Summary

      Constructors 
      Constructor Description
      LoggingTestKit()  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      static LoggingTestKit custom​(scala.Function1<LoggingEvent,​java.lang.Object> test)
      Create a custom event filter.
      static LoggingTestKit deadLetters()
      Filter for the logging of dead letters.
      static LoggingTestKit debug​(java.lang.String messageIncludes)
      Create a filter for DEBUG level events with a log message that contains the given messageIncludes.
      static LoggingTestKit empty()
      An empty filter that doesn't match any events.
      static LoggingTestKit error​(java.lang.Class<? extends java.lang.Throwable> causeClass)
      Create a filter for ERROR level events with a an included throwable that is a class or subclass of the given * Throwable class.
      static LoggingTestKit error​(java.lang.String messageIncludes)
      Create a filter for ERROR level events with a log message that contains the given messageIncludes.
      abstract <T> T expect​(ActorSystem<?> system, java.util.function.Supplier<T> code)
      Run the given code block and assert that the criteria of this LoggingTestKit has matched within the configured akka.actor.testkit.typed.filter-leeway as often as requested by its occurrences parameter specifies.
      static LoggingTestKit info​(java.lang.String messageIncludes)
      Create a filter for INFO level events with a log message that contains the given messageIncludes.
      abstract boolean matches​(LoggingEvent event)  
      static LoggingTestKit messageContains​(java.lang.String str)
      Create a filter for events with a log message that contains the given messageIncludes.
      static LoggingTestKit trace​(java.lang.String messageIncludes)
      Create a filter for TRACE level events with a log message that contains the given messageIncludes.
      static LoggingTestKit warn​(java.lang.Class<java.lang.Throwable> causeClass)
      Create a filter for WARN level events with a an included throwable that is a class or subclass of the given * Throwable class.
      static LoggingTestKit warn​(java.lang.String messageIncludes)
      Create a filter for WARN level events with a log message that contains the given messageIncludes.
      abstract LoggingTestKit withCause​(java.lang.Class<? extends java.lang.Throwable> newCause)
      Matching events with an included throwable that is a class or subclass of the given Throwable class.
      abstract LoggingTestKit withCheckExcess​(boolean check)
      After matching the expected number of hits, check for excess messages
      abstract LoggingTestKit withCustom​(scala.Function1<LoggingEvent,​java.lang.Object> newCustom)
      Matching events for which the supplied function returns true.
      abstract LoggingTestKit withLoggerName​(java.lang.String newLoggerName)
      Matching events with the given logger name or sub-names in the same way as configuration loggers are configured in logback.xml.
      abstract LoggingTestKit withLogLevel​(org.slf4j.event.Level newLogLevel)
      Matching events with the given log level.
      abstract LoggingTestKit withMdc​(java.util.Map<java.lang.String,​java.lang.String> newMdc)
      Matching events with MDC containing all entries of the given Map.
      abstract LoggingTestKit withMessageContains​(java.lang.String newMessageContains)
      Matching events with a message that contains the given value.
      abstract LoggingTestKit withMessageRegex​(java.lang.String newMessageRegex)
      Matching events with a message that matches the given regular expression.
      abstract LoggingTestKit withOccurrences​(int newOccurrences)
      Number of events the testkit is supposed to match.
      abstract LoggingTestKit withSource​(java.lang.String newSource)
      Matching events that have "akkaSource" MDC value equal to the given value.
      • Methods inherited from class java.lang.Object

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

      • LoggingTestKit

        public LoggingTestKit()
    • Method Detail

      • empty

        public static LoggingTestKit empty()
        An empty filter that doesn't match any events.

        More conditions can be added to the returned [LoggingEventFilter].

      • messageContains

        public static LoggingTestKit messageContains​(java.lang.String str)
        Create a filter for events with a log message that contains the given messageIncludes.

        More conditions can be added to the returned [LoggingEventFilter].

      • trace

        public static LoggingTestKit trace​(java.lang.String messageIncludes)
        Create a filter for TRACE level events with a log message that contains the given messageIncludes.

        More conditions can be added to the returned [LoggingEventFilter].

      • debug

        public static LoggingTestKit debug​(java.lang.String messageIncludes)
        Create a filter for DEBUG level events with a log message that contains the given messageIncludes.

        More conditions can be added to the returned [LoggingEventFilter].

      • info

        public static LoggingTestKit info​(java.lang.String messageIncludes)
        Create a filter for INFO level events with a log message that contains the given messageIncludes.

        More conditions can be added to the returned [LoggingEventFilter].

      • warn

        public static LoggingTestKit warn​(java.lang.String messageIncludes)
        Create a filter for WARN level events with a log message that contains the given messageIncludes.

        More conditions can be added to the returned [LoggingEventFilter].

      • warn

        public static LoggingTestKit warn​(java.lang.Class<java.lang.Throwable> causeClass)
        Create a filter for WARN level events with a an included throwable that is a class or subclass of the given * Throwable class.

        More conditions can be added to the returned [LoggingEventFilter].

      • error

        public static LoggingTestKit error​(java.lang.String messageIncludes)
        Create a filter for ERROR level events with a log message that contains the given messageIncludes.

        More conditions can be added to the returned [LoggingEventFilter].

      • error

        public static LoggingTestKit error​(java.lang.Class<? extends java.lang.Throwable> causeClass)
        Create a filter for ERROR level events with a an included throwable that is a class or subclass of the given * Throwable class.

        More conditions can be added to the returned [LoggingEventFilter].

      • custom

        public static LoggingTestKit custom​(scala.Function1<LoggingEvent,​java.lang.Object> test)
        Create a custom event filter. The filter will match those events for which for which the supplied function returns true.
      • deadLetters

        public static LoggingTestKit deadLetters()
        Filter for the logging of dead letters.
      • withOccurrences

        public abstract LoggingTestKit withOccurrences​(int newOccurrences)
        Number of events the testkit is supposed to match. By default 1.

        When occurrences > 0 it will not look for excess messages that are logged asynchronously outside (after) the intercept thunk and it has already found expected number.

        When occurrences is 0 it will look for unexpected matching events, and then it will also look for excess messages during the configured akka.actor.testkit.typed.expect-no-message-default duration.

      • withLogLevel

        public abstract LoggingTestKit withLogLevel​(org.slf4j.event.Level newLogLevel)
        Matching events with the given log level.
      • withLoggerName

        public abstract LoggingTestKit withLoggerName​(java.lang.String newLoggerName)
        Matching events with the given logger name or sub-names in the same way as configuration loggers are configured in logback.xml. By default the root logger is used.
      • withSource

        public abstract LoggingTestKit withSource​(java.lang.String newSource)
        Matching events that have "akkaSource" MDC value equal to the given value. "akkaSource" is typically the actor path.
      • withMessageContains

        public abstract LoggingTestKit withMessageContains​(java.lang.String newMessageContains)
        Matching events with a message that contains the given value.
      • withMessageRegex

        public abstract LoggingTestKit withMessageRegex​(java.lang.String newMessageRegex)
        Matching events with a message that matches the given regular expression.
      • withCause

        public abstract LoggingTestKit withCause​(java.lang.Class<? extends java.lang.Throwable> newCause)
        Matching events with an included throwable that is a class or subclass of the given Throwable class.
      • withMdc

        public abstract LoggingTestKit withMdc​(java.util.Map<java.lang.String,​java.lang.String> newMdc)
        Matching events with MDC containing all entries of the given Map. The event MDC may have more entries than the given Map.
      • withCheckExcess

        public abstract LoggingTestKit withCheckExcess​(boolean check)
        After matching the expected number of hits, check for excess messages
      • withCustom

        public abstract LoggingTestKit withCustom​(scala.Function1<LoggingEvent,​java.lang.Object> newCustom)
        Matching events for which the supplied function returns true.
      • matches

        public abstract boolean matches​(LoggingEvent event)
        Returns:
        true if the event matches the conditions of the filter.
      • expect

        public abstract <T> T expect​(ActorSystem<?> system,
                                     java.util.function.Supplier<T> code)
        Run the given code block and assert that the criteria of this LoggingTestKit has matched within the configured akka.actor.testkit.typed.filter-leeway as often as requested by its occurrences parameter specifies.

        Care is taken to remove the testkit when the block is finished or aborted.