Skip to content

Commit dca6851

Browse files
committed
Added tests for producer-consumer pattern
1 parent 42a1dc6 commit dca6851

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

producer-consumer/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@
1414
<artifactId>junit</artifactId>
1515
<scope>test</scope>
1616
</dependency>
17+
<dependency>
18+
<groupId>org.mockito</groupId>
19+
<artifactId>mockito-core</artifactId>
20+
<scope>test</scope>
21+
</dependency>
1722
</dependencies>
1823
</project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.iluwatar.producer.consumer;
2+
3+
import org.junit.Test;
4+
import org.mockito.InOrder;
5+
6+
import static org.mockito.Mockito.inOrder;
7+
import static org.mockito.Mockito.reset;
8+
import static org.mockito.Mockito.spy;
9+
10+
/**
11+
* Date: 12/27/15 - 11:01 PM
12+
*
13+
* @author Jeroen Meulemeester
14+
*/
15+
public class ConsumerTest extends StdOutTest {
16+
17+
private static final int ITEM_COUNT = 5;
18+
19+
@Test
20+
public void testConsume() throws Exception {
21+
final ItemQueue queue = spy(new ItemQueue());
22+
for (int id = 0; id < ITEM_COUNT; id++) {
23+
queue.put(new Item("producer", id));
24+
}
25+
26+
reset(queue); // Don't count the preparation above as interactions with the queue
27+
final Consumer consumer = new Consumer("consumer", queue);
28+
29+
final InOrder inOrder = inOrder(getStdOutMock());
30+
for (int id = 0; id < ITEM_COUNT; id++) {
31+
consumer.consume();
32+
inOrder.verify(getStdOutMock())
33+
.println("Consumer [consumer] consume item [" + id + "] produced by [producer]");
34+
}
35+
36+
inOrder.verifyNoMoreInteractions();
37+
}
38+
39+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.iluwatar.producer.consumer;
2+
3+
import org.junit.Test;
4+
5+
import static org.mockito.Matchers.any;
6+
import static org.mockito.Mockito.mock;
7+
import static org.mockito.Mockito.verify;
8+
import static org.mockito.Mockito.verifyNoMoreInteractions;
9+
10+
/**
11+
* Date: 12/28/15 - 12:12 AM
12+
*
13+
* @author Jeroen Meulemeester
14+
*/
15+
public class ProducerTest {
16+
17+
@Test(timeout = 6000)
18+
public void testProduce() throws Exception {
19+
final ItemQueue queue = mock(ItemQueue.class);
20+
final Producer producer = new Producer("producer", queue);
21+
22+
producer.produce();
23+
verify(queue).put(any(Item.class));
24+
25+
verifyNoMoreInteractions(queue);
26+
}
27+
28+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.iluwatar.producer.consumer;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
6+
import java.io.PrintStream;
7+
8+
import static org.mockito.Mockito.mock;
9+
10+
/**
11+
* Date: 12/10/15 - 8:37 PM
12+
*
13+
* @author Jeroen Meulemeester
14+
*/
15+
public abstract class StdOutTest {
16+
17+
/**
18+
* The mocked standard out {@link PrintStream}, required since some actions don't have any
19+
* influence on accessible objects, except for writing to std-out using {@link System#out}
20+
*/
21+
private final PrintStream stdOutMock = mock(PrintStream.class);
22+
23+
/**
24+
* Keep the original std-out so it can be restored after the test
25+
*/
26+
private final PrintStream stdOutOrig = System.out;
27+
28+
/**
29+
* Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test
30+
*/
31+
@Before
32+
public void setUp() {
33+
System.setOut(this.stdOutMock);
34+
}
35+
36+
/**
37+
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
38+
*/
39+
@After
40+
public void tearDown() {
41+
System.setOut(this.stdOutOrig);
42+
}
43+
44+
/**
45+
* Get the mocked stdOut {@link PrintStream}
46+
*
47+
* @return The stdOut print stream mock, renewed before each test
48+
*/
49+
final PrintStream getStdOutMock() {
50+
return this.stdOutMock;
51+
}
52+
53+
}

0 commit comments

Comments
 (0)