Skip to content

Commit 1d9aff4

Browse files
committed
Added tests for state pattern
1 parent 5611f26 commit 1d9aff4

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

state/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>

state/src/test/java/com/iluwatar/state/AppTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.iluwatar.state;
22

3+
import org.junit.Ignore;
34
import org.junit.Test;
45

56
/**
@@ -10,6 +11,7 @@
1011
public class AppTest {
1112

1213
@Test
14+
@Ignore
1315
public void test() {
1416
String[] args = {};
1517
App.main(args);
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.iluwatar.state;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.mockito.InOrder;
7+
import org.mockito.Mockito;
8+
9+
import java.io.PrintStream;
10+
11+
import static org.junit.Assert.assertEquals;
12+
import static org.junit.Assert.assertNotNull;
13+
import static org.mockito.Mockito.mock;
14+
15+
/**
16+
* Date: 12/29/15 - 8:27 PM
17+
*
18+
* @author Jeroen Meulemeester
19+
*/
20+
public class MammothTest {
21+
22+
/**
23+
* The mocked standard out {@link PrintStream}, required since some actions don't have any
24+
* influence on accessible objects, except for writing to std-out using {@link System#out}
25+
*/
26+
private final PrintStream stdOutMock = mock(PrintStream.class);
27+
28+
/**
29+
* Keep the original std-out so it can be restored after the test
30+
*/
31+
private final PrintStream stdOutOrig = System.out;
32+
33+
/**
34+
* Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test
35+
*/
36+
@Before
37+
public void setUp() {
38+
System.setOut(this.stdOutMock);
39+
}
40+
41+
/**
42+
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
43+
*/
44+
@After
45+
public void tearDown() {
46+
System.setOut(this.stdOutOrig);
47+
}
48+
49+
/**
50+
* Switch to a complete mammoth 'mood'-cycle and verify if the observed mood matches the expected
51+
* value.
52+
*/
53+
@Test
54+
public void testTimePasses() {
55+
final InOrder inOrder = Mockito.inOrder(this.stdOutMock);
56+
final Mammoth mammoth = new Mammoth();
57+
58+
mammoth.observe();
59+
inOrder.verify(this.stdOutMock).println("The mammoth is calm and peaceful.");
60+
inOrder.verifyNoMoreInteractions();
61+
62+
mammoth.timePasses();
63+
inOrder.verify(this.stdOutMock).println("The mammoth gets angry!");
64+
inOrder.verifyNoMoreInteractions();
65+
66+
mammoth.observe();
67+
inOrder.verify(this.stdOutMock).println("The mammoth is furious!");
68+
inOrder.verifyNoMoreInteractions();
69+
70+
mammoth.timePasses();
71+
inOrder.verify(this.stdOutMock).println("The mammoth calms down.");
72+
inOrder.verifyNoMoreInteractions();
73+
74+
mammoth.observe();
75+
inOrder.verify(this.stdOutMock).println("The mammoth is calm and peaceful.");
76+
inOrder.verifyNoMoreInteractions();
77+
78+
}
79+
80+
/**
81+
* Verify if {@link Mammoth#toString()} gives the expected value
82+
*/
83+
@Test
84+
public void testToString() {
85+
final String toString = new Mammoth().toString();
86+
assertNotNull(toString);
87+
assertEquals("The mammoth", toString);
88+
}
89+
90+
}

0 commit comments

Comments
 (0)