#502 Adjusted tests for logger introduction
This commit is contained in:
@ -22,17 +22,19 @@
|
||||
*/
|
||||
package com.iluwatar.state;
|
||||
|
||||
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.mockito.InOrder;
|
||||
import org.mockito.Mockito;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Date: 12/29/15 - 8:27 PM
|
||||
@ -41,31 +43,16 @@ import static org.mockito.Mockito.mock;
|
||||
*/
|
||||
public class MammothTest {
|
||||
|
||||
/**
|
||||
* The mocked standard out {@link PrintStream}, required since some actions don't have any
|
||||
* influence on accessible objects, 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();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
|
||||
*/
|
||||
@After
|
||||
public void tearDown() {
|
||||
System.setOut(this.stdOutOrig);
|
||||
appender.stop();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -74,28 +61,27 @@ public class MammothTest {
|
||||
*/
|
||||
@Test
|
||||
public void testTimePasses() {
|
||||
final InOrder inOrder = Mockito.inOrder(this.stdOutMock);
|
||||
final Mammoth mammoth = new Mammoth();
|
||||
|
||||
mammoth.observe();
|
||||
inOrder.verify(this.stdOutMock).println("The mammoth is calm and peaceful.");
|
||||
inOrder.verifyNoMoreInteractions();
|
||||
assertEquals("The mammoth is calm and peaceful.", appender.getLastMessage());
|
||||
assertEquals(1 , appender.getLogSize());
|
||||
|
||||
mammoth.timePasses();
|
||||
inOrder.verify(this.stdOutMock).println("The mammoth gets angry!");
|
||||
inOrder.verifyNoMoreInteractions();
|
||||
assertEquals("The mammoth gets angry!", appender.getLastMessage());
|
||||
assertEquals(2 , appender.getLogSize());
|
||||
|
||||
mammoth.observe();
|
||||
inOrder.verify(this.stdOutMock).println("The mammoth is furious!");
|
||||
inOrder.verifyNoMoreInteractions();
|
||||
assertEquals("The mammoth is furious!", appender.getLastMessage());
|
||||
assertEquals(3 , appender.getLogSize());
|
||||
|
||||
mammoth.timePasses();
|
||||
inOrder.verify(this.stdOutMock).println("The mammoth calms down.");
|
||||
inOrder.verifyNoMoreInteractions();
|
||||
assertEquals("The mammoth calms down.", appender.getLastMessage());
|
||||
assertEquals(4 , appender.getLogSize());
|
||||
|
||||
mammoth.observe();
|
||||
inOrder.verify(this.stdOutMock).println("The mammoth is calm and peaceful.");
|
||||
inOrder.verifyNoMoreInteractions();
|
||||
assertEquals("The mammoth is calm and peaceful.", appender.getLastMessage());
|
||||
assertEquals(5 , appender.getLogSize());
|
||||
|
||||
}
|
||||
|
||||
@ -109,4 +95,26 @@ public class MammothTest {
|
||||
assertEquals("The mammoth", toString);
|
||||
}
|
||||
|
||||
}
|
||||
private class InMemoryAppender extends AppenderBase<ILoggingEvent> {
|
||||
private List<ILoggingEvent> log = new LinkedList<>();
|
||||
|
||||
public InMemoryAppender() {
|
||||
((Logger) LoggerFactory.getLogger("root")).addAppender(this);
|
||||
start();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void append(ILoggingEvent eventObject) {
|
||||
log.add(eventObject);
|
||||
}
|
||||
|
||||
public int getLogSize() {
|
||||
return log.size();
|
||||
}
|
||||
|
||||
public String getLastMessage() {
|
||||
return log.get(log.size() - 1).getFormattedMessage();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user