Skip to content

Commit 47709e2

Browse files
committed
Added tests for template-method pattern
1 parent 09d3a82 commit 47709e2

File tree

5 files changed

+243
-0
lines changed

5 files changed

+243
-0
lines changed

template-method/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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.iluwatar.templatemethod;
2+
3+
import org.junit.Test;
4+
5+
import static org.mockito.Mockito.mock;
6+
import static org.mockito.Mockito.verify;
7+
import static org.mockito.Mockito.verifyNoMoreInteractions;
8+
9+
/**
10+
* Date: 12/29/15 - 18:15 PM
11+
*
12+
* @author Jeroen Meulemeester
13+
*/
14+
public class HalflingThiefTest {
15+
16+
/**
17+
* Verify if the thief uses the provided stealing method
18+
*/
19+
@Test
20+
public void testSteal() {
21+
final StealingMethod method = mock(StealingMethod.class);
22+
final HalflingThief thief = new HalflingThief(method);
23+
24+
thief.steal();
25+
verify(method).steal();
26+
27+
verifyNoMoreInteractions(method);
28+
}
29+
30+
/**
31+
* Verify if the thief uses the provided stealing method, and the new method after changing it
32+
*/
33+
@Test
34+
public void testChangeMethod() {
35+
final StealingMethod initialMethod = mock(StealingMethod.class);
36+
final HalflingThief thief = new HalflingThief(initialMethod);
37+
38+
thief.steal();
39+
verify(initialMethod).steal();
40+
41+
final StealingMethod newMethod = mock(StealingMethod.class);
42+
thief.changeMethod(newMethod);
43+
44+
thief.steal();
45+
verify(newMethod).steal();
46+
47+
verifyNoMoreInteractions(initialMethod, newMethod);
48+
49+
}
50+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.iluwatar.templatemethod;
2+
3+
/**
4+
* Date: 12/30/15 - 18:12 PM
5+
*
6+
* @author Jeroen Meulemeester
7+
*/
8+
public class HitAndRunMethodTest extends StealingMethodTest<HitAndRunMethod> {
9+
10+
/**
11+
* Create a new test for the {@link HitAndRunMethod}
12+
*/
13+
public HitAndRunMethodTest() {
14+
super(
15+
new HitAndRunMethod(),
16+
"old goblin woman",
17+
"The target has been chosen as old goblin woman.",
18+
"Approach the old goblin woman from behind.",
19+
"Grab the handbag and run away fast!"
20+
);
21+
}
22+
23+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package com.iluwatar.templatemethod;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.mockito.InOrder;
7+
8+
import java.io.PrintStream;
9+
10+
import static org.junit.Assert.assertEquals;
11+
import static org.mockito.Mockito.inOrder;
12+
import static org.mockito.Mockito.mock;
13+
import static org.mockito.Mockito.verify;
14+
import static org.mockito.Mockito.verifyNoMoreInteractions;
15+
import static org.mockito.Mockito.verifyZeroInteractions;
16+
17+
/**
18+
* Date: 12/30/15 - 18:12 PM
19+
*
20+
* @author Jeroen Meulemeester
21+
*/
22+
public abstract class StealingMethodTest<M extends StealingMethod> {
23+
24+
/**
25+
* The tested stealing method
26+
*/
27+
private final M method;
28+
29+
/**
30+
* The expected target
31+
*/
32+
private final String expectedTarget;
33+
34+
/**
35+
* The expected target picking result
36+
*/
37+
private final String expectedTargetResult;
38+
39+
/**
40+
* The expected confusion method
41+
*/
42+
private final String expectedConfuseMethod;
43+
44+
/**
45+
* The expected stealing method
46+
*/
47+
private final String expectedStealMethod;
48+
49+
/**
50+
* The mocked standard out {@link PrintStream}, required since some actions don't have any
51+
* influence on accessible objects, except for writing to std-out using {@link System#out}
52+
*/
53+
private final PrintStream stdOutMock = mock(PrintStream.class);
54+
55+
/**
56+
* Keep the original std-out so it can be restored after the test
57+
*/
58+
private final PrintStream stdOutOrig = System.out;
59+
60+
/**
61+
* Create a new test for the given stealing method, together with the expected results
62+
*
63+
* @param method The tested stealing method
64+
* @param expectedTarget The expected target name
65+
* @param expectedTargetResult The expected target picking result
66+
* @param expectedConfuseMethod The expected confusion method
67+
* @param expectedStealMethod The expected stealing method
68+
*/
69+
public StealingMethodTest(final M method, String expectedTarget, final String expectedTargetResult,
70+
final String expectedConfuseMethod, final String expectedStealMethod) {
71+
72+
this.method = method;
73+
this.expectedTarget = expectedTarget;
74+
this.expectedTargetResult = expectedTargetResult;
75+
this.expectedConfuseMethod = expectedConfuseMethod;
76+
this.expectedStealMethod = expectedStealMethod;
77+
}
78+
79+
/**
80+
* Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test
81+
*/
82+
@Before
83+
public void setUp() {
84+
System.setOut(this.stdOutMock);
85+
}
86+
87+
/**
88+
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
89+
*/
90+
@After
91+
public void tearDown() {
92+
System.setOut(this.stdOutOrig);
93+
}
94+
95+
/**
96+
* Verify if the thief picks the correct target
97+
*/
98+
@Test
99+
public void testPickTarget() {
100+
assertEquals(expectedTarget, this.method.pickTarget());
101+
}
102+
103+
/**
104+
* Verify if the target confusing step goes as planned
105+
*/
106+
@Test
107+
public void testConfuseTarget() {
108+
verifyZeroInteractions(this.stdOutMock);
109+
110+
this.method.confuseTarget(this.expectedTarget);
111+
verify(this.stdOutMock).println(this.expectedConfuseMethod);
112+
verifyNoMoreInteractions(this.stdOutMock);
113+
}
114+
115+
/**
116+
* Verify if the stealing step goes as planned
117+
*/
118+
@Test
119+
public void testStealTheItem() {
120+
verifyZeroInteractions(this.stdOutMock);
121+
122+
this.method.stealTheItem(this.expectedTarget);
123+
verify(this.stdOutMock).println(this.expectedStealMethod);
124+
verifyNoMoreInteractions(this.stdOutMock);
125+
}
126+
127+
/**
128+
* Verify if the complete steal process goes as planned
129+
*/
130+
@Test
131+
public void testSteal() {
132+
final InOrder inOrder = inOrder(this.stdOutMock);
133+
134+
this.method.steal();
135+
136+
inOrder.verify(this.stdOutMock).println(this.expectedTargetResult);
137+
inOrder.verify(this.stdOutMock).println(this.expectedConfuseMethod);
138+
inOrder.verify(this.stdOutMock).println(this.expectedStealMethod);
139+
inOrder.verifyNoMoreInteractions();
140+
}
141+
142+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.iluwatar.templatemethod;
2+
3+
/**
4+
* Date: 12/30/15 - 18:19 PM
5+
*
6+
* @author Jeroen Meulemeester
7+
*/
8+
public class SubtleMethodTest extends StealingMethodTest<SubtleMethod> {
9+
10+
/**
11+
* Create a new test for the {@link SubtleMethod}
12+
*/
13+
public SubtleMethodTest() {
14+
super(
15+
new SubtleMethod(),
16+
"shop keeper",
17+
"The target has been chosen as shop keeper.",
18+
"Approach the shop keeper with tears running and hug him!",
19+
"While in close contact grab the shop keeper's wallet."
20+
);
21+
}
22+
23+
}

0 commit comments

Comments
 (0)