#502 Adjusted tests for logger introduction
This commit is contained in:
@ -22,15 +22,19 @@
|
||||
*/
|
||||
package com.iluwatar.model.view.controller;
|
||||
|
||||
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.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
|
||||
/**
|
||||
* Date: 12/20/15 - 2:04 PM
|
||||
@ -39,32 +43,16 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
*/
|
||||
public class GiantViewTest {
|
||||
|
||||
/**
|
||||
* The mocked standard out {@link PrintStream}, required since the actions of the views don't 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);
|
||||
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(GiantView.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
|
||||
*/
|
||||
@After
|
||||
public void tearDown() {
|
||||
System.setOut(this.stdOutOrig);
|
||||
appender.stop();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -78,9 +66,29 @@ public class GiantViewTest {
|
||||
final GiantModel model = mock(GiantModel.class);
|
||||
view.displayGiant(model);
|
||||
|
||||
verify(this.stdOutMock).println(model);
|
||||
verifyNoMoreInteractions(model, this.stdOutMock);
|
||||
|
||||
assertEquals(model.toString(), appender.getLastMessage());
|
||||
assertEquals(1, appender.getLogSize());
|
||||
}
|
||||
|
||||
}
|
||||
public 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).getMessage();
|
||||
}
|
||||
|
||||
public int getLogSize() {
|
||||
return log.size();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user