1 package mockit;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.junit.jupiter.api.Assertions.assertFalse;
5 import static org.junit.jupiter.api.Assertions.assertNotNull;
6
7 import java.text.DateFormat;
8 import java.text.SimpleDateFormat;
9 import java.util.Date;
10
11 import org.junit.jupiter.api.Test;
12
13
14
15
16 final class MockingNewInstancesWithVaryingBehaviorTest {
17
18
19 static final String DATE_FORMAT = "yyyy-MM-dd";
20
21
22 static final String FORMATTED_DATE = "2012-02-28";
23
24
25 static final String TIME_FORMAT = "HH";
26
27
28 static final String FORMATTED_TIME = "13";
29
30
31
32
33 void exerciseAndVerifyTestedCode() {
34 Date now = new Date();
35
36 String hour = new SimpleDateFormat(TIME_FORMAT).format(now);
37 assertEquals(FORMATTED_TIME, hour);
38
39 String date = new SimpleDateFormat(DATE_FORMAT).format(now);
40 assertEquals(FORMATTED_DATE, date);
41 }
42
43
44
45
46
47 DateFormat dateFormat;
48
49
50 DateFormat hourFormat;
51
52
53
54
55 @Test
56 void usingFakesWithInvocationParameter() {
57 new MockUp<SimpleDateFormat>() {
58 @Mock
59 void $init(Invocation inv, String pattern) {
60 DateFormat dt = inv.getInvokedInstance();
61 if (DATE_FORMAT.equals(pattern)) {
62 dateFormat = dt;
63 } else if (TIME_FORMAT.equals(pattern)) {
64 hourFormat = dt;
65 }
66 }
67 };
68
69 new MockUp<DateFormat>() {
70 @Mock
71 String format(Invocation inv, Date d) {
72 assertNotNull(d);
73 DateFormat dt = inv.getInvokedInstance();
74 if (dt == dateFormat) {
75 return FORMATTED_DATE;
76 }
77 if (dt == hourFormat) {
78 return FORMATTED_TIME;
79 } else {
80 return null;
81 }
82 }
83 };
84
85 exerciseAndVerifyTestedCode();
86 }
87
88
89
90
91
92
93
94
95
96
97 @Test
98 void usingInstantiationRecording(@Mocked SimpleDateFormat anyDateFormat) {
99 new Expectations() {
100 {
101 SimpleDateFormat dateFmt = new SimpleDateFormat(DATE_FORMAT);
102 dateFmt.format((Date) any);
103 result = FORMATTED_DATE;
104
105 SimpleDateFormat hourFmt = new SimpleDateFormat(TIME_FORMAT);
106 hourFmt.format((Date) any);
107 result = FORMATTED_TIME;
108 }
109 };
110
111 exerciseAndVerifyTestedCode();
112 }
113
114
115
116
117 static class Collaborator {
118
119
120 final int value;
121
122
123
124
125 Collaborator() {
126 value = -1;
127 }
128
129
130
131
132
133
134
135 Collaborator(int value) {
136 this.value = value;
137 }
138
139
140
141
142
143
144 int getValue() {
145 return value;
146 }
147
148
149
150
151
152
153 boolean isPositive() {
154 return value > 0;
155 }
156 }
157
158
159 Collaborator col;
160
161
162 Collaborator col5;
163
164
165 Collaborator col6;
166
167
168
169
170
171
172
173 @Test
174 void matchMethodCallsOnInstancesCreatedWithSpecificConstructorCalls(@Mocked final Collaborator anyCol) {
175 new Expectations() {
176 {
177 col5 = new Collaborator(5);
178 col5.getValue();
179 result = 123;
180 times = 2;
181
182 col = new Collaborator();
183 times = 1;
184 col6 = new Collaborator(6);
185 }
186 };
187
188 assertEquals(0, new Collaborator().getValue());
189
190 Collaborator newCol1 = new Collaborator(5);
191 assertEquals(123, newCol1.getValue());
192
193 Collaborator newCol2 = new Collaborator(6);
194 assertEquals(0, newCol2.getValue());
195 assertFalse(newCol2.isPositive());
196
197 Collaborator newCol3 = new Collaborator(5);
198 assertEquals(123, newCol3.getValue());
199 assertFalse(newCol3.isPositive());
200
201 new Verifications() {
202 {
203 new Collaborator(anyInt);
204 times = 3;
205 col.getValue();
206 times = 1;
207 col6.getValue();
208 times = 1;
209 col5.isPositive();
210 times = 1;
211 col6.isPositive();
212 times = 1;
213 }
214 };
215 }
216
217
218
219
220
221
222
223 @Test
224 void recordDifferentResultsForInstancesCreatedWithDifferentConstructors(@Mocked final Collaborator anyCol) {
225 new Expectations() {
226 {
227 anyCol.getValue();
228 result = 1;
229
230 Collaborator col2 = new Collaborator(anyInt);
231 col2.getValue();
232 result = 2;
233 }
234 };
235
236 int valueFromRecordedConstructor = new Collaborator(10).getValue();
237 int valueFromAnyOtherConstructor = new Collaborator().getValue();
238
239 assertEquals(2, valueFromRecordedConstructor);
240 assertEquals(1, valueFromAnyOtherConstructor);
241 }
242 }