Class LogCaptureRule

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

    public class LogCaptureRule
    extends Object
    implements org.junit.rules.TestRule
    JUnit Rule which "captures" slf4j-simple logs. By default, it fails the test if an error was logged. It can also pass the test if a logged error was expected (but fail it otherwise).

    Usage:

       public @Rule LogCaptureRule logCaptureRule = new LogCaptureRule();
    
       @Test ...
    
           logRule.expectError();
     

    See also e.g. slf4j-test or slf4jtesting (both of which, contrary to this, don't integrate with slf4j-simple which we already widely use in ODL tests).

    Author:
    Michael Vorburger.ch
    See Also:
    ExpectedException
    • Constructor Detail

      • LogCaptureRule

        public LogCaptureRule()
    • Method Detail

      • apply

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

        public void handleErrorLogs​(Consumer<com.google.common.collect.ImmutableList<LogCapture>> newErrorLogHandler)
      • expectLastErrorMessageContains

        public void expectLastErrorMessageContains​(String partialErrorMessage)
      • expectError

        public void expectError​(String message,
                                int howManyMessagesBack)
      • expectError

        public void expectError​(String message)
      • getLastErrorThrowable

        public Throwable getLastErrorThrowable()
      • getErrorThrowable

        public Throwable getErrorThrowable​(int howManyMessagesBack)