Added tests for producer-consumer pattern
This commit is contained in:
@ -14,5 +14,10 @@
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@ -0,0 +1,39 @@
|
||||
package com.iluwatar.producer.consumer;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.InOrder;
|
||||
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.reset;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
/**
|
||||
* Date: 12/27/15 - 11:01 PM
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
public class ConsumerTest extends StdOutTest {
|
||||
|
||||
private static final int ITEM_COUNT = 5;
|
||||
|
||||
@Test
|
||||
public void testConsume() throws Exception {
|
||||
final ItemQueue queue = spy(new ItemQueue());
|
||||
for (int id = 0; id < ITEM_COUNT; id++) {
|
||||
queue.put(new Item("producer", id));
|
||||
}
|
||||
|
||||
reset(queue); // Don't count the preparation above as interactions with the queue
|
||||
final Consumer consumer = new Consumer("consumer", queue);
|
||||
|
||||
final InOrder inOrder = inOrder(getStdOutMock());
|
||||
for (int id = 0; id < ITEM_COUNT; id++) {
|
||||
consumer.consume();
|
||||
inOrder.verify(getStdOutMock())
|
||||
.println("Consumer [consumer] consume item [" + id + "] produced by [producer]");
|
||||
}
|
||||
|
||||
inOrder.verifyNoMoreInteractions();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.iluwatar.producer.consumer;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
|
||||
/**
|
||||
* Date: 12/28/15 - 12:12 AM
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
public class ProducerTest {
|
||||
|
||||
@Test(timeout = 6000)
|
||||
public void testProduce() throws Exception {
|
||||
final ItemQueue queue = mock(ItemQueue.class);
|
||||
final Producer producer = new Producer("producer", queue);
|
||||
|
||||
producer.produce();
|
||||
verify(queue).put(any(Item.class));
|
||||
|
||||
verifyNoMoreInteractions(queue);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package com.iluwatar.producer.consumer;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user