Uploaded image for project: 'SLF4J'
  1. SLF4J
  2. SLF4J-461

Unit testing support

    XMLWordPrintable

Details

    • Icon: New Feature New Feature
    • Resolution: Unresolved
    • Icon: Major Major
    • None
    • 1.7.25, 1.8.0-beta4
    • Unspecified

    Description

      I would like to submit some code to help write unit tests that verify log behavior. Examples:

      • Make sure that some code correctly logs errors and warnings
      • The log messages contain the correct information (instead of, say, "[byte").
      • Files are logged with absolute path
      • Remove (expected) stack traces from test output

      My code contains a class RedirectLogger which takes lists of classes or logger names and adds appenders to those which collect log events in a list.

      At the end of the test, you can call a dump() method (optionally with a filter) to get everything that was logged.

      Example code before Java 8:

      RedirectLogger rl = new RedirectLogger(Tool.class, Util.class);
      try {
          rl.install();
      
          tool.foo();
      } finally  {
          rl.deinstall();
      }
      
      assertEquals("Expected log lines", rl.dump(Level.WARN));
      

      Example > Java 8 with method that returns value:

      RedirectLogger rl = new RedirectLogger(Tool.class, Util.class);
      Type result = rl.with(() -> {
          return tool.bar();
      });
      
      assertEquals(..., result);
      assertEquals("Expected log lines", rl.dump(Level.WARN));

       

      Attachments

        Activity

          People

            slf4j-dev SLF4J developers list
            adigulla Aaron Digulla
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: