SHARE
TWEET

Untitled

a guest Oct 21st, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import org.slf4j.LoggerFactory;
  2. import ch.qos.logback.classic.Level;
  3. import ch.qos.logback.classic.Logger;
  4. import ch.qos.logback.classic.spi.ILoggingEvent;
  5. import ch.qos.logback.core.read.ListAppender;
  6.  
  7. public class FooTest {
  8.  
  9.     @Test
  10.     void doThat() throws Exception {
  11.         // get Logback Logger
  12.         Logger fooLogger = (Logger) LoggerFactory.getLogger(Foo.class);
  13.  
  14.         // create and start a ListAppender
  15.         ListAppender<ILoggingEvent> listAppender = new ListAppender<>();
  16.         listAppender.start();
  17.  
  18.         // add the appender to the logger
  19.         fooLogger.addAppender(listAppender);
  20.  
  21.         // call method under test
  22.         Foo foo = new Foo();
  23.         foo.doThat();
  24.  
  25.         // JUnit assertions
  26.         List<ILoggingEvent> logsList = listAppender.list;
  27.         assertEquals("start", logsList.get(0)
  28.                                       .getMessage());
  29.         assertEquals(Level.INFO, logsList.get(0)
  30.                                          .getLevel());
  31.  
  32.         assertEquals("finish", logsList.get(1)
  33.                                        .getMessage());
  34.         assertEquals(Level.INFO, logsList.get(1)
  35.                                          .getLevel());
  36.     }
  37. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top