#502 Adjusted tests for logger introduction
This commit is contained in:
@ -22,19 +22,19 @@
|
||||
*/
|
||||
package com.iluwatar.templatemethod;
|
||||
|
||||
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.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Date: 12/30/15 - 18:12 PM
|
||||
@ -43,6 +43,18 @@ import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
*/
|
||||
public abstract class StealingMethodTest<M extends StealingMethod> {
|
||||
|
||||
private InMemoryAppender appender;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
appender = new InMemoryAppender();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
appender.stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* The tested stealing method
|
||||
*/
|
||||
@ -68,17 +80,6 @@ public abstract class StealingMethodTest<M extends StealingMethod> {
|
||||
*/
|
||||
private final String expectedStealMethod;
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* Keep the original std-out so it can be restored after the test
|
||||
*/
|
||||
private final PrintStream stdOutOrig = System.out;
|
||||
|
||||
/**
|
||||
* Create a new test for the given stealing method, together with the expected results
|
||||
*
|
||||
@ -98,22 +99,6 @@ public abstract class StealingMethodTest<M extends StealingMethod> {
|
||||
this.expectedStealMethod = expectedStealMethod;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test
|
||||
*/
|
||||
@Before
|
||||
public void setUp() {
|
||||
System.setOut(this.stdOutMock);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
|
||||
*/
|
||||
@After
|
||||
public void tearDown() {
|
||||
System.setOut(this.stdOutOrig);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if the thief picks the correct target
|
||||
*/
|
||||
@ -127,11 +112,11 @@ public abstract class StealingMethodTest<M extends StealingMethod> {
|
||||
*/
|
||||
@Test
|
||||
public void testConfuseTarget() {
|
||||
verifyZeroInteractions(this.stdOutMock);
|
||||
assertEquals(0, appender.getLogSize());
|
||||
|
||||
this.method.confuseTarget(this.expectedTarget);
|
||||
verify(this.stdOutMock).println(this.expectedConfuseMethod);
|
||||
verifyNoMoreInteractions(this.stdOutMock);
|
||||
assertEquals(this.expectedConfuseMethod, appender.getLastMessage());
|
||||
assertEquals(1, appender.getLogSize());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -139,11 +124,11 @@ public abstract class StealingMethodTest<M extends StealingMethod> {
|
||||
*/
|
||||
@Test
|
||||
public void testStealTheItem() {
|
||||
verifyZeroInteractions(this.stdOutMock);
|
||||
assertEquals(0, appender.getLogSize());
|
||||
|
||||
this.method.stealTheItem(this.expectedTarget);
|
||||
verify(this.stdOutMock).println(this.expectedStealMethod);
|
||||
verifyNoMoreInteractions(this.stdOutMock);
|
||||
assertEquals(this.expectedStealMethod, appender.getLastMessage());
|
||||
assertEquals(1, appender.getLogSize());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -151,14 +136,37 @@ public abstract class StealingMethodTest<M extends StealingMethod> {
|
||||
*/
|
||||
@Test
|
||||
public void testSteal() {
|
||||
final InOrder inOrder = inOrder(this.stdOutMock);
|
||||
|
||||
this.method.steal();
|
||||
|
||||
inOrder.verify(this.stdOutMock).println(this.expectedTargetResult);
|
||||
inOrder.verify(this.stdOutMock).println(this.expectedConfuseMethod);
|
||||
inOrder.verify(this.stdOutMock).println(this.expectedStealMethod);
|
||||
inOrder.verifyNoMoreInteractions();
|
||||
assertTrue(appender.logContains(this.expectedTargetResult));
|
||||
assertTrue(appender.logContains(this.expectedConfuseMethod));
|
||||
assertTrue(appender.logContains(this.expectedStealMethod));
|
||||
assertEquals(3, appender.getLogSize());
|
||||
}
|
||||
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
public boolean logContains(String message) {
|
||||
return log.stream().anyMatch(event -> event.getFormattedMessage().equals(message));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user