View Javadoc
1   package com.github.valfirst.slf4jtest;
2   
3   import org.junit.rules.TestRule;
4   import org.junit.runner.Description;
5   import org.junit.runners.model.Statement;
6   
7   /**
8    * A <a href="https://github.com/junit-team/junit/wiki">JUnit</a> rule that clears the ThreadLocal
9    * state of all the TestLoggers and the TestLoggerFactory.
10   */
11  public class TestLoggerFactoryResetRule implements TestRule {
12  
13      @Override
14      public Statement apply(final Statement base, final Description description) {
15          return new TestLoggerFactoryResettingStatement(base);
16      }
17  
18      private static class TestLoggerFactoryResettingStatement extends Statement {
19          private final Statement base;
20  
21          public TestLoggerFactoryResettingStatement(final Statement base) {
22              super();
23              this.base = base;
24          }
25  
26          @Override
27          public void evaluate() throws Throwable {
28              TestLoggerFactory.clear();
29              try {
30                  base.evaluate();
31              } finally {
32                  TestLoggerFactory.clear();
33              }
34          }
35      }
36  }