1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.easymock.tests2; |
18 |
|
|
19 |
|
import static org.easymock.EasyMock.*; |
20 |
|
import static org.junit.Assert.*; |
21 |
|
|
22 |
|
import org.easymock.tests.IMethods; |
23 |
|
import org.junit.Before; |
24 |
|
import org.junit.Test; |
25 |
|
|
26 |
|
|
27 |
|
@author |
28 |
|
|
|
|
| 90,4% |
Uncovered Elements: 15 (156) |
Complexity: 30 |
Complexity Density: 0,22 |
|
29 |
|
public class UsageTest { |
30 |
|
|
31 |
|
IMethods mock; |
32 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
33 |
26
|
@Before... |
34 |
|
public void setup() { |
35 |
26
|
mock = createMock(IMethods.class); |
36 |
|
} |
37 |
|
|
|
|
| 83,3% |
Uncovered Elements: 2 (12) |
Complexity: 3 |
Complexity Density: 0,3 |
1
PASS
|
|
38 |
2
|
@Test... |
39 |
|
public void exactCallCountByLastCall() { |
40 |
2
|
expect(mock.oneArg(false)).andReturn("Test").andReturn("Test2"); |
41 |
2
|
replay(mock); |
42 |
|
|
43 |
2
|
assertEquals("Test", mock.oneArg(false)); |
44 |
2
|
assertEquals("Test2", mock.oneArg(false)); |
45 |
|
|
46 |
2
|
boolean failed = false; |
47 |
2
|
try { |
48 |
2
|
mock.oneArg(false); |
49 |
|
} catch (final AssertionError expected) { |
50 |
2
|
failed = true; |
51 |
|
} |
52 |
2
|
if (!failed) |
53 |
0
|
fail("expected AssertionError"); |
54 |
|
} |
55 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0,2 |
1
PASS
|
|
56 |
2
|
@Test... |
57 |
|
public void openCallCountByLastCall() { |
58 |
2
|
expect(mock.oneArg(false)).andReturn("Test").andReturn("Test2").atLeastOnce(); |
59 |
|
|
60 |
2
|
replay(mock); |
61 |
|
|
62 |
2
|
assertEquals("Test", mock.oneArg(false)); |
63 |
2
|
assertEquals("Test2", mock.oneArg(false)); |
64 |
2
|
assertEquals("Test2", mock.oneArg(false)); |
65 |
|
} |
66 |
|
|
|
|
| 73,3% |
Uncovered Elements: 4 (15) |
Complexity: 4 |
Complexity Density: 0,31 |
1
PASS
|
|
67 |
2
|
@Test... |
68 |
|
public void exactCallCountByLastThrowable() { |
69 |
2
|
expect(mock.oneArg(false)).andReturn("Test").andReturn("Test2").andThrow( |
70 |
|
new IndexOutOfBoundsException()); |
71 |
|
|
72 |
2
|
replay(mock); |
73 |
|
|
74 |
2
|
assertEquals("Test", mock.oneArg(false)); |
75 |
2
|
assertEquals("Test2", mock.oneArg(false)); |
76 |
|
|
77 |
2
|
try { |
78 |
2
|
mock.oneArg(false); |
79 |
0
|
fail(); |
80 |
|
} catch (final IndexOutOfBoundsException expected) { |
81 |
|
} |
82 |
|
|
83 |
2
|
boolean failed = true; |
84 |
2
|
try { |
85 |
2
|
mock.oneArg(false); |
86 |
0
|
failed = false; |
87 |
|
} catch (final AssertionError expected) { |
88 |
|
} |
89 |
2
|
if (!failed) |
90 |
0
|
fail("expected AssertionError"); |
91 |
|
} |
92 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 3 |
Complexity Density: 0,38 |
1
PASS
|
|
93 |
2
|
@Test... |
94 |
|
public void openCallCountByLastThrowable() { |
95 |
2
|
expect(mock.oneArg(false)).andReturn("Test").andReturn("Test2").andThrow( |
96 |
|
new IndexOutOfBoundsException()).atLeastOnce(); |
97 |
|
|
98 |
2
|
replay(mock); |
99 |
|
|
100 |
2
|
assertEquals("Test", mock.oneArg(false)); |
101 |
2
|
assertEquals("Test2", mock.oneArg(false)); |
102 |
|
|
103 |
2
|
try { |
104 |
2
|
mock.oneArg(false); |
105 |
|
} catch (final IndexOutOfBoundsException expected) { |
106 |
|
} |
107 |
2
|
try { |
108 |
2
|
mock.oneArg(false); |
109 |
|
} catch (final IndexOutOfBoundsException expected) { |
110 |
|
} |
111 |
|
} |
112 |
|
|
|
|
| 75% |
Uncovered Elements: 3 (12) |
Complexity: 3 |
Complexity Density: 0,3 |
1
PASS
|
|
113 |
2
|
@Test... |
114 |
|
public void moreThanOneArgument() { |
115 |
2
|
expect(mock.threeArgumentMethod(1, "2", "3")).andReturn("Test").times(2); |
116 |
|
|
117 |
2
|
replay(mock); |
118 |
|
|
119 |
2
|
assertEquals("Test", mock.threeArgumentMethod(1, "2", "3")); |
120 |
|
|
121 |
2
|
boolean failed = true; |
122 |
2
|
try { |
123 |
2
|
verify(mock); |
124 |
0
|
failed = false; |
125 |
|
} catch (final AssertionError expected) { |
126 |
2
|
assertEquals("\n Expectation failure on verify:" |
127 |
|
+ "\n threeArgumentMethod(1, \"2\", \"3\"): expected: 2, actual: 1", expected |
128 |
|
.getMessage()); |
129 |
|
} |
130 |
2
|
if (!failed) { |
131 |
0
|
fail("exception expected"); |
132 |
|
} |
133 |
|
} |
134 |
|
|
|
|
| 83,3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0,33 |
1
PASS
|
|
135 |
2
|
@Test... |
136 |
|
public void wrongArguments() { |
137 |
2
|
mock.simpleMethodWithArgument("3"); |
138 |
2
|
replay(mock); |
139 |
|
|
140 |
2
|
try { |
141 |
2
|
mock.simpleMethodWithArgument("5"); |
142 |
0
|
fail(); |
143 |
|
} catch (final AssertionError expected) { |
144 |
2
|
assertEquals("\n Unexpected method call simpleMethodWithArgument(\"5\"):" |
145 |
|
+ "\n simpleMethodWithArgument(\"3\"): expected: 1, actual: 0", expected.getMessage()); |
146 |
|
} |
147 |
|
|
148 |
|
} |
149 |
|
|
|
|
| 85,7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0,29 |
1
PASS
|
|
150 |
2
|
@Test... |
151 |
|
public void summarizeSameObjectArguments() { |
152 |
2
|
mock.simpleMethodWithArgument("3"); |
153 |
2
|
mock.simpleMethodWithArgument("3"); |
154 |
2
|
replay(mock); |
155 |
|
|
156 |
2
|
try { |
157 |
2
|
mock.simpleMethodWithArgument("5"); |
158 |
0
|
fail(); |
159 |
|
} catch (final AssertionError expected) { |
160 |
2
|
assertEquals("\n Unexpected method call simpleMethodWithArgument(\"5\"):" |
161 |
|
+ "\n simpleMethodWithArgument(\"3\"): expected: 2, actual: 0", expected.getMessage()); |
162 |
|
} |
163 |
|
|
164 |
|
} |
165 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 2 |
Complexity Density: 0,2 |
1
PASS
|
|
166 |
2
|
@Test... |
167 |
|
public void argumentsOrdered() { |
168 |
2
|
mock.simpleMethodWithArgument("4"); |
169 |
2
|
mock.simpleMethodWithArgument("3"); |
170 |
2
|
mock.simpleMethodWithArgument("2"); |
171 |
2
|
mock.simpleMethodWithArgument("0"); |
172 |
2
|
mock.simpleMethodWithArgument("1"); |
173 |
2
|
replay(mock); |
174 |
|
|
175 |
2
|
try { |
176 |
2
|
mock.simpleMethodWithArgument("5"); |
177 |
0
|
fail("exception expected"); |
178 |
|
} catch (final AssertionError expected) { |
179 |
2
|
assertEquals("\n Unexpected method call simpleMethodWithArgument(\"5\"):" |
180 |
|
+ "\n simpleMethodWithArgument(\"4\"): expected: 1, actual: 0" |
181 |
|
+ "\n simpleMethodWithArgument(\"3\"): expected: 1, actual: 0" |
182 |
|
+ "\n simpleMethodWithArgument(\"2\"): expected: 1, actual: 0" |
183 |
|
+ "\n simpleMethodWithArgument(\"0\"): expected: 1, actual: 0" |
184 |
|
+ "\n simpleMethodWithArgument(\"1\"): expected: 1, actual: 0", expected.getMessage()); |
185 |
|
} |
186 |
|
|
187 |
|
} |
188 |
|
|
|
|
| 92,3% |
Uncovered Elements: 2 (26) |
Complexity: 3 |
Complexity Density: 0,12 |
1
PASS
|
|
189 |
2
|
@Test... |
190 |
|
public void mixingOrderedAndUnordered() { |
191 |
2
|
mock.simpleMethodWithArgument("2"); |
192 |
2
|
mock.simpleMethodWithArgument("1"); |
193 |
2
|
checkOrder(mock, true); |
194 |
2
|
mock.simpleMethodWithArgument("3"); |
195 |
2
|
mock.simpleMethodWithArgument("4"); |
196 |
2
|
checkOrder(mock, false); |
197 |
2
|
mock.simpleMethodWithArgument("6"); |
198 |
2
|
mock.simpleMethodWithArgument("7"); |
199 |
2
|
mock.simpleMethodWithArgument("5"); |
200 |
|
|
201 |
2
|
replay(mock); |
202 |
|
|
203 |
2
|
mock.simpleMethodWithArgument("1"); |
204 |
2
|
mock.simpleMethodWithArgument("2"); |
205 |
|
|
206 |
2
|
boolean failed = false; |
207 |
2
|
try { |
208 |
2
|
mock.simpleMethodWithArgument("4"); |
209 |
|
} catch (final AssertionError e) { |
210 |
2
|
failed = true; |
211 |
|
} |
212 |
2
|
if (!failed) { |
213 |
0
|
fail(); |
214 |
|
} |
215 |
|
|
216 |
2
|
mock.simpleMethodWithArgument("3"); |
217 |
2
|
mock.simpleMethodWithArgument("4"); |
218 |
2
|
mock.simpleMethodWithArgument("5"); |
219 |
2
|
mock.simpleMethodWithArgument("6"); |
220 |
2
|
mock.simpleMethodWithArgument("7"); |
221 |
|
|
222 |
2
|
verify(mock); |
223 |
|
|
224 |
|
} |
225 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0,25 |
1
PASS
|
|
226 |
2
|
@Test... |
227 |
|
public void resumeIfFailure() { |
228 |
2
|
final IMethods mock = createMock(IMethods.class); |
229 |
2
|
expect(mock.oneArg(true)).andReturn("foo").anyTimes(); |
230 |
2
|
replay(mock); |
231 |
|
|
232 |
2
|
mock.oneArg(true); |
233 |
|
|
234 |
2
|
try { |
235 |
2
|
mock.simpleMethod(); |
236 |
|
} catch (final AssertionError error) { |
237 |
|
} |
238 |
|
|
239 |
2
|
mock.oneArg(true); |
240 |
|
|
241 |
2
|
verify(mock); |
242 |
|
} |
243 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0,14 |
1
PASS
|
|
244 |
2
|
@Test... |
245 |
|
public void defaultResetToNice() { |
246 |
2
|
final IMethods mock = createMock(IMethods.class); |
247 |
|
|
248 |
2
|
expect(mock.oneArg(true)).andReturn("foo"); |
249 |
2
|
replay(mock); |
250 |
|
|
251 |
2
|
resetToNice(mock); |
252 |
|
|
253 |
2
|
replay(mock); |
254 |
|
|
255 |
2
|
assertNull(mock.oneArg(true)); |
256 |
|
|
257 |
2
|
verify(mock); |
258 |
|
} |
259 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0,09 |
1
PASS
|
|
260 |
2
|
@Test... |
261 |
|
public void strictResetToDefault() { |
262 |
2
|
final IMethods mock = createStrictMock(IMethods.class); |
263 |
|
|
264 |
2
|
expect(mock.oneArg(true)).andReturn("foo"); |
265 |
2
|
expect(mock.oneArg(false)).andReturn("foo"); |
266 |
|
|
267 |
2
|
replay(mock); |
268 |
|
|
269 |
2
|
resetToDefault(mock); |
270 |
|
|
271 |
2
|
expect(mock.oneArg(false)).andReturn("foo"); |
272 |
2
|
expect(mock.oneArg(true)).andReturn("foo"); |
273 |
|
|
274 |
2
|
replay(mock); |
275 |
|
|
276 |
2
|
assertEquals("foo", mock.oneArg(false)); |
277 |
2
|
assertEquals("foo", mock.oneArg(true)); |
278 |
|
|
279 |
2
|
verify(mock); |
280 |
|
} |
281 |
|
|
|
|
| 92,9% |
Uncovered Elements: 1 (14) |
Complexity: 2 |
Complexity Density: 0,14 |
1
PASS
|
|
282 |
2
|
@Test... |
283 |
|
public void niceToStrict() { |
284 |
2
|
final IMethods mock = createNiceMock(IMethods.class); |
285 |
|
|
286 |
2
|
expect(mock.oneArg(false)).andReturn("foo"); |
287 |
|
|
288 |
2
|
replay(mock); |
289 |
|
|
290 |
2
|
assertNull(mock.oneArg(true)); |
291 |
|
|
292 |
2
|
resetToStrict(mock); |
293 |
|
|
294 |
2
|
expect(mock.oneArg(false)).andReturn("foo"); |
295 |
2
|
expect(mock.oneArg(true)).andReturn("foo"); |
296 |
|
|
297 |
2
|
replay(mock); |
298 |
|
|
299 |
2
|
try { |
300 |
2
|
mock.oneArg(true); |
301 |
0
|
fail(); |
302 |
|
} catch (final AssertionError e) { |
303 |
|
} |
304 |
|
|
305 |
2
|
assertEquals("foo", mock.oneArg(false)); |
306 |
2
|
assertEquals("foo", mock.oneArg(true)); |
307 |
|
|
308 |
2
|
verify(mock); |
309 |
|
} |
310 |
|
} |