#502 Adjusted tests for logger introduction
This commit is contained in:
@ -22,18 +22,31 @@
|
||||
*/
|
||||
package com.iluwatar.privateclassdata;
|
||||
|
||||
import com.iluwatar.privateclassdata.utils.InMemoryAppender;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InOrder;
|
||||
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Date: 12/27/15 - 10:46 PM
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
public class ImmutableStewTest extends StdOutTest {
|
||||
public class ImmutableStewTest {
|
||||
|
||||
private InMemoryAppender appender;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
appender = new InMemoryAppender();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
appender.stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if mixing the stew doesn't change the internal state
|
||||
@ -41,15 +54,14 @@ public class ImmutableStewTest extends StdOutTest {
|
||||
@Test
|
||||
public void testMix() {
|
||||
final Stew stew = new Stew(1, 2, 3, 4);
|
||||
final String message = "Mixing the stew we find: 1 potatoes, 2 carrots, 3 meat and 4 peppers";
|
||||
final String expectedMessage = "Mixing the stew we find: 1 potatoes, 2 carrots, 3 meat and 4 peppers";
|
||||
|
||||
final InOrder inOrder = inOrder(getStdOutMock());
|
||||
for (int i = 0; i < 20; i++) {
|
||||
stew.mix();
|
||||
inOrder.verify(getStdOutMock()).println(message);
|
||||
assertEquals(expectedMessage, appender.getLastMessage());
|
||||
}
|
||||
|
||||
inOrder.verifyNoMoreInteractions();
|
||||
assertEquals(20, appender.getLogSize());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -60,15 +72,12 @@ public class ImmutableStewTest extends StdOutTest {
|
||||
final Stew stew = new Stew(1, 2, 3, 4);
|
||||
stew.mix();
|
||||
|
||||
verify(getStdOutMock())
|
||||
.println("Mixing the stew we find: 1 potatoes, 2 carrots, 3 meat and 4 peppers");
|
||||
assertEquals("Mixing the stew we find: 1 potatoes, 2 carrots, 3 meat and 4 peppers", appender.getLastMessage());
|
||||
|
||||
stew.taste();
|
||||
verify(getStdOutMock()).println("Tasting the stew");
|
||||
assertEquals("Tasting the stew", appender.getLastMessage());
|
||||
|
||||
stew.mix();
|
||||
verify(getStdOutMock())
|
||||
.println("Mixing the stew we find: 0 potatoes, 1 carrots, 2 meat and 3 peppers");
|
||||
|
||||
assertEquals("Mixing the stew we find: 0 potatoes, 1 carrots, 2 meat and 3 peppers", appender.getLastMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,75 +0,0 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.privateclassdata;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
||||
import java.io.PrintStream;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Date: 12/10/15 - 8:37 PM
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
public abstract class StdOutTest {
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mocked stdOut {@link PrintStream}
|
||||
*
|
||||
* @return The stdOut print stream mock, renewed before each test
|
||||
*/
|
||||
final PrintStream getStdOutMock() {
|
||||
return this.stdOutMock;
|
||||
}
|
||||
|
||||
}
|
@ -22,17 +22,31 @@
|
||||
*/
|
||||
package com.iluwatar.privateclassdata;
|
||||
|
||||
import com.iluwatar.privateclassdata.utils.InMemoryAppender;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InOrder;
|
||||
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Date: 12/27/15 - 10:46 PM
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
public class StewTest extends StdOutTest {
|
||||
public class StewTest {
|
||||
|
||||
private InMemoryAppender appender;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
appender = new InMemoryAppender();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
appender.stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if mixing the stew doesn't change the internal state
|
||||
@ -43,13 +57,12 @@ public class StewTest extends StdOutTest {
|
||||
final String expectedMessage = "Mixing the immutable stew we find: 1 potatoes, "
|
||||
+ "2 carrots, 3 meat and 4 peppers";
|
||||
|
||||
final InOrder inOrder = inOrder(getStdOutMock());
|
||||
for (int i = 0; i < 20; i++) {
|
||||
stew.mix();
|
||||
inOrder.verify(getStdOutMock()).println(expectedMessage);
|
||||
assertEquals(expectedMessage, appender.getLastMessage());
|
||||
}
|
||||
|
||||
inOrder.verifyNoMoreInteractions();
|
||||
assertEquals(20, appender.getLogSize());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,53 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.privateclassdata.utils;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.AppenderBase;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public 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