#502 Adjusted tests for logger introduction
This commit is contained in:
		@@ -22,17 +22,18 @@
 | 
			
		||||
 */
 | 
			
		||||
package com.iluwatar.event.aggregator;
 | 
			
		||||
 | 
			
		||||
import ch.qos.logback.classic.Logger;
 | 
			
		||||
import ch.qos.logback.classic.spi.ILoggingEvent;
 | 
			
		||||
import ch.qos.logback.core.AppenderBase;
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.slf4j.LoggerFactory;
 | 
			
		||||
 | 
			
		||||
import java.io.PrintStream;
 | 
			
		||||
import java.util.LinkedList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import static org.mockito.Mockito.mock;
 | 
			
		||||
import static org.mockito.Mockito.times;
 | 
			
		||||
import static org.mockito.Mockito.verify;
 | 
			
		||||
import static org.mockito.Mockito.verifyZeroInteractions;
 | 
			
		||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
 | 
			
		||||
import static org.junit.Assert.assertEquals;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Date: 12/12/15 - 3:04 PM
 | 
			
		||||
@@ -41,31 +42,16 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
 | 
			
		||||
 */
 | 
			
		||||
public class KingJoffreyTest {
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * The mocked standard out {@link PrintStream}, required since {@link KingJoffrey} does nothing
 | 
			
		||||
   * except for writing to std-out using {@link System#out}
 | 
			
		||||
   */
 | 
			
		||||
  private final PrintStream stdOutMock = mock(PrintStream.class);
 | 
			
		||||
  private InMemoryAppender appender;
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Keep the original std-out so it can be restored after the test
 | 
			
		||||
   */
 | 
			
		||||
  private final PrintStream stdOutOrig = System.out;
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test
 | 
			
		||||
   */
 | 
			
		||||
  @Before
 | 
			
		||||
  public void setUp() {
 | 
			
		||||
    System.setOut(this.stdOutMock);
 | 
			
		||||
    appender = new InMemoryAppender(KingJoffrey.class);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Removed the mocked std-out {@link PrintStream} again from the {@link System} class
 | 
			
		||||
   */
 | 
			
		||||
  @After
 | 
			
		||||
  public void tearDown() {
 | 
			
		||||
    System.setOut(this.stdOutOrig);
 | 
			
		||||
    appender.stop();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
@@ -75,15 +61,38 @@ public class KingJoffreyTest {
 | 
			
		||||
  public void testOnEvent() {
 | 
			
		||||
    final KingJoffrey kingJoffrey = new KingJoffrey();
 | 
			
		||||
 | 
			
		||||
    for (final Event event : Event.values()) {
 | 
			
		||||
      verifyZeroInteractions(this.stdOutMock);
 | 
			
		||||
    for (int i = 0; i < Event.values().length; ++i) {
 | 
			
		||||
      assertEquals(i, appender.getLogSize());
 | 
			
		||||
      Event event = Event.values()[i];
 | 
			
		||||
      kingJoffrey.onEvent(event);
 | 
			
		||||
 | 
			
		||||
      final String expectedMessage = "Received event from the King's Hand: " + event.toString();
 | 
			
		||||
      verify(this.stdOutMock, times(1)).println(expectedMessage);
 | 
			
		||||
      verifyNoMoreInteractions(this.stdOutMock);
 | 
			
		||||
      assertEquals(expectedMessage, appender.getLastMessage());
 | 
			
		||||
      assertEquals(i + 1, appender.getLogSize());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
  private class InMemoryAppender extends AppenderBase<ILoggingEvent> {
 | 
			
		||||
    private List<ILoggingEvent> log = new LinkedList<>();
 | 
			
		||||
 | 
			
		||||
    public InMemoryAppender(Class clazz) {
 | 
			
		||||
      ((Logger) LoggerFactory.getLogger(clazz)).addAppender(this);
 | 
			
		||||
      start();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void append(ILoggingEvent eventObject) {
 | 
			
		||||
      log.add(eventObject);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String getLastMessage() {
 | 
			
		||||
      return log.get(log.size() - 1).getFormattedMessage();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getLogSize() {
 | 
			
		||||
      return log.size();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user