#502 Adjusted tests for logger introduction
This commit is contained in:
@ -22,22 +22,25 @@
|
||||
*/
|
||||
package com.iluwatar.mediator;
|
||||
|
||||
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.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
|
||||
/**
|
||||
* Date: 12/19/15 - 10:13 PM
|
||||
@ -57,34 +60,6 @@ public class PartyMemberTest {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The mocked standard out {@link PrintStream}, required since some actions on a {@link
|
||||
* PartyMember} have any influence on any other 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;
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* The factory, used to create a new instance of the tested party member
|
||||
*/
|
||||
@ -99,6 +74,18 @@ public class PartyMemberTest {
|
||||
this.memberSupplier = memberSupplier;
|
||||
}
|
||||
|
||||
private InMemoryAppender appender;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
appender = new InMemoryAppender(PartyMemberBase.class);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
appender.stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if a party action triggers the correct output to the std-Out
|
||||
*/
|
||||
@ -108,10 +95,10 @@ public class PartyMemberTest {
|
||||
|
||||
for (final Action action : Action.values()) {
|
||||
member.partyAction(action);
|
||||
verify(this.stdOutMock).println(member.toString() + " " + action.getDescription());
|
||||
assertEquals(member.toString() + " " + action.getDescription(), appender.getLastMessage());
|
||||
}
|
||||
|
||||
verifyNoMoreInteractions(this.stdOutMock);
|
||||
assertEquals(Action.values().length, appender.getLogSize());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -122,19 +109,19 @@ public class PartyMemberTest {
|
||||
final PartyMember member = this.memberSupplier.get();
|
||||
|
||||
member.act(Action.GOLD);
|
||||
verifyZeroInteractions(this.stdOutMock);
|
||||
assertEquals(0, appender.getLogSize());
|
||||
|
||||
final Party party = mock(Party.class);
|
||||
member.joinedParty(party);
|
||||
verify(this.stdOutMock).println(member.toString() + " joins the party");
|
||||
assertEquals(member.toString() + " joins the party", appender.getLastMessage());
|
||||
|
||||
for (final Action action : Action.values()) {
|
||||
member.act(action);
|
||||
verify(this.stdOutMock).println(member.toString() + " " + action.toString());
|
||||
assertEquals(member.toString() + " " + action.toString(), appender.getLastMessage());
|
||||
verify(party).act(member, action);
|
||||
}
|
||||
|
||||
verifyNoMoreInteractions(party, this.stdOutMock);
|
||||
assertEquals(Action.values().length + 1, appender.getLogSize());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -147,4 +134,27 @@ public class PartyMemberTest {
|
||||
assertEquals(memberClass.getSimpleName(), member.toString());
|
||||
}
|
||||
|
||||
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 int getLogSize() {
|
||||
return log.size();
|
||||
}
|
||||
|
||||
public String getLastMessage() {
|
||||
return log.get(log.size() - 1).getFormattedMessage();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user