Skip to content

Commit 42a1dc6

Browse files
committed
Added tests for private-class-data pattern
1 parent 47e1cd7 commit 42a1dc6

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed

private-class-data/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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.iluwatar.privateclassdata;
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.verify;
8+
9+
/**
10+
* Date: 12/27/15 - 10:46 PM
11+
*
12+
* @author Jeroen Meulemeester
13+
*/
14+
public class ImmutableStewTest extends StdOutTest {
15+
16+
/**
17+
* Verify if mixing the stew doesn't change the internal state
18+
*/
19+
@Test
20+
public void testMix() {
21+
final Stew stew = new Stew(1, 2, 3, 4);
22+
final String message = "Mixing the stew we find: 1 potatoes, 2 carrots, 3 meat and 4 peppers";
23+
24+
final InOrder inOrder = inOrder(getStdOutMock());
25+
for (int i = 0; i < 20; i++) {
26+
stew.mix();
27+
inOrder.verify(getStdOutMock()).println(message);
28+
}
29+
30+
inOrder.verifyNoMoreInteractions();
31+
}
32+
33+
/**
34+
* Verify if tasting the stew actually removes one of each ingredient
35+
*/
36+
@Test
37+
public void testDrink() {
38+
final Stew stew = new Stew(1, 2, 3, 4);
39+
stew.mix();
40+
41+
verify(getStdOutMock())
42+
.println("Mixing the stew we find: 1 potatoes, 2 carrots, 3 meat and 4 peppers");
43+
44+
stew.taste();
45+
verify(getStdOutMock()).println("Tasting the stew");
46+
47+
stew.mix();
48+
verify(getStdOutMock())
49+
.println("Mixing the stew we find: 0 potatoes, 1 carrots, 2 meat and 3 peppers");
50+
51+
}
52+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.iluwatar.privateclassdata;
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+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.iluwatar.privateclassdata;
2+
3+
import org.junit.Test;
4+
import org.mockito.InOrder;
5+
6+
import static org.mockito.Mockito.inOrder;
7+
8+
/**
9+
* Date: 12/27/15 - 10:46 PM
10+
*
11+
* @author Jeroen Meulemeester
12+
*/
13+
public class StewTest extends StdOutTest {
14+
15+
/**
16+
* Verify if mixing the stew doesn't change the internal state
17+
*/
18+
@Test
19+
public void testMix() {
20+
final ImmutableStew stew = new ImmutableStew(1, 2, 3, 4);
21+
final String expectedMessage = "Mixing the immutable stew we find: 1 potatoes, " +
22+
"2 carrots, 3 meat and 4 peppers";
23+
24+
final InOrder inOrder = inOrder(getStdOutMock());
25+
for (int i = 0; i < 20; i++) {
26+
stew.mix();
27+
inOrder.verify(getStdOutMock()).println(expectedMessage);
28+
}
29+
30+
inOrder.verifyNoMoreInteractions();
31+
}
32+
33+
}

0 commit comments

Comments
 (0)