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+ }
0 commit comments