1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.easymock.tests; |
18 |
|
|
19 |
|
import static org.easymock.EasyMock.*; |
20 |
|
import static org.junit.Assert.*; |
21 |
|
|
22 |
|
import org.easymock.internal.ReplayState; |
23 |
|
import org.junit.Before; |
24 |
|
import org.junit.Test; |
25 |
|
|
26 |
|
|
27 |
|
@author |
28 |
|
|
|
|
| 87,6% |
Uncovered Elements: 16 (129) |
Complexity: 26 |
Complexity Density: 0,25 |
|
29 |
|
public class UsageStrictMockTest { |
30 |
|
|
31 |
|
private IMethods mock; |
32 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0,25 |
|
33 |
18
|
@Before... |
34 |
|
public void setup() { |
35 |
18
|
mock = createStrictMock(IMethods.class); |
36 |
|
|
37 |
18
|
mock.simpleMethodWithArgument("1"); |
38 |
18
|
mock.simpleMethodWithArgument("2"); |
39 |
|
|
40 |
18
|
replay(mock); |
41 |
|
} |
42 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
1
PASS
|
|
43 |
2
|
@Test... |
44 |
|
public void testVerify() { |
45 |
2
|
reset(mock); |
46 |
2
|
replay(mock); |
47 |
2
|
verify(mock); |
48 |
|
} |
49 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
1
PASS
|
|
50 |
2
|
@Test... |
51 |
|
public void orderedCallsSucces() { |
52 |
2
|
mock.simpleMethodWithArgument("1"); |
53 |
2
|
mock.simpleMethodWithArgument("2"); |
54 |
|
|
55 |
2
|
verify(mock); |
56 |
|
} |
57 |
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 3 |
Complexity Density: 0,5 |
1
PASS
|
|
58 |
2
|
@Test... |
59 |
|
public void unorderedCallsFailure() { |
60 |
2
|
boolean failed = false; |
61 |
2
|
try { |
62 |
2
|
mock.simpleMethodWithArgument("2"); |
63 |
|
} catch (final AssertionError expected) { |
64 |
2
|
failed = true; |
65 |
|
} |
66 |
2
|
if (!failed) { |
67 |
0
|
fail("unordered calls accepted"); |
68 |
|
} |
69 |
|
} |
70 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 3 |
Complexity Density: 0,38 |
1
PASS
|
|
71 |
2
|
@Test... |
72 |
|
public void tooManyCallsFailure() { |
73 |
2
|
mock.simpleMethodWithArgument("1"); |
74 |
2
|
mock.simpleMethodWithArgument("2"); |
75 |
|
|
76 |
2
|
boolean failed = false; |
77 |
2
|
try { |
78 |
2
|
mock.simpleMethodWithArgument("2"); |
79 |
|
} catch (final AssertionError expected) { |
80 |
2
|
failed = true; |
81 |
|
} |
82 |
2
|
if (!failed) { |
83 |
0
|
fail("too many calls accepted"); |
84 |
|
} |
85 |
|
} |
86 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 3 |
Complexity Density: 0,38 |
1
PASS
|
|
87 |
2
|
@Test... |
88 |
|
public void tooFewCallsFailure() { |
89 |
2
|
mock.simpleMethodWithArgument("1"); |
90 |
2
|
boolean failed = false; |
91 |
2
|
try { |
92 |
2
|
verify(mock); |
93 |
|
} catch (final AssertionError expected) { |
94 |
2
|
failed = true; |
95 |
2
|
assertTrue("stack trace must be filled in", Util.getStackTrace(expected).indexOf( |
96 |
|
ReplayState.class.getName()) == -1); |
97 |
|
} |
98 |
2
|
if (!failed) { |
99 |
0
|
fail("too few calls accepted"); |
100 |
|
} |
101 |
|
} |
102 |
|
|
|
|
| 85,7% |
Uncovered Elements: 4 (28) |
Complexity: 5 |
Complexity Density: 0,21 |
1
PASS
|
|
103 |
2
|
@Test... |
104 |
|
public void differentMethods() { |
105 |
|
|
106 |
2
|
reset(mock); |
107 |
|
|
108 |
2
|
expect(mock.booleanReturningMethod(0)).andReturn(true); |
109 |
2
|
mock.simpleMethod(); |
110 |
2
|
expect(mock.booleanReturningMethod(1)).andReturn(false).times(2, 3); |
111 |
2
|
mock.simpleMethod(); |
112 |
2
|
expectLastCall().atLeastOnce(); |
113 |
|
|
114 |
2
|
replay(mock); |
115 |
2
|
assertEquals(true, mock.booleanReturningMethod(0)); |
116 |
2
|
mock.simpleMethod(); |
117 |
|
|
118 |
2
|
boolean failed = false; |
119 |
2
|
try { |
120 |
2
|
verify(mock); |
121 |
|
} catch (final AssertionError expected) { |
122 |
2
|
failed = true; |
123 |
2
|
assertEquals("\n Expectation failure on verify:" |
124 |
|
+ "\n simpleMethod(): expected: 1, actual: 1" |
125 |
|
+ "\n booleanReturningMethod(1): expected: between 2 and 3, actual: 0" |
126 |
|
+ "\n simpleMethod(): expected: at least 1, actual: 0", expected.getMessage()); |
127 |
|
} |
128 |
2
|
if (!failed) { |
129 |
0
|
fail("too few calls accepted"); |
130 |
|
} |
131 |
|
|
132 |
2
|
assertEquals(false, mock.booleanReturningMethod(1)); |
133 |
|
|
134 |
2
|
failed = false; |
135 |
2
|
try { |
136 |
2
|
mock.simpleMethod(); |
137 |
|
} catch (final AssertionError expected) { |
138 |
2
|
failed = true; |
139 |
2
|
assertEquals("\n Unexpected method call simpleMethod():" |
140 |
|
+ "\n booleanReturningMethod(1): expected: between 2 and 3, actual: 1", expected |
141 |
|
.getMessage()); |
142 |
|
} |
143 |
2
|
if (!failed) { |
144 |
0
|
fail("wrong call accepted"); |
145 |
|
} |
146 |
|
} |
147 |
|
|
|
|
| 90,9% |
Uncovered Elements: 2 (22) |
Complexity: 3 |
Complexity Density: 0,15 |
1
PASS
|
|
148 |
2
|
@Test... |
149 |
|
public void range() { |
150 |
|
|
151 |
2
|
reset(mock); |
152 |
|
|
153 |
2
|
expect(mock.booleanReturningMethod(0)).andReturn(true); |
154 |
2
|
mock.simpleMethod(); |
155 |
2
|
expect(mock.booleanReturningMethod(1)).andReturn(false).times(2, 3); |
156 |
2
|
mock.simpleMethod(); |
157 |
2
|
expectLastCall().atLeastOnce(); |
158 |
2
|
expect(mock.booleanReturningMethod(1)).andReturn(false); |
159 |
|
|
160 |
2
|
replay(mock); |
161 |
|
|
162 |
2
|
mock.booleanReturningMethod(0); |
163 |
2
|
mock.simpleMethod(); |
164 |
|
|
165 |
2
|
mock.booleanReturningMethod(1); |
166 |
2
|
mock.booleanReturningMethod(1); |
167 |
2
|
mock.booleanReturningMethod(1); |
168 |
|
|
169 |
2
|
boolean failed = false; |
170 |
|
|
171 |
2
|
try { |
172 |
2
|
mock.booleanReturningMethod(1); |
173 |
|
} catch (final AssertionError expected) { |
174 |
2
|
failed = true; |
175 |
2
|
assertEquals("\n Unexpected method call booleanReturningMethod(1):" |
176 |
|
+ "\n booleanReturningMethod(1): expected: between 2 and 3, actual: 4" |
177 |
|
+ "\n simpleMethod(): expected: at least 1, actual: 0", expected.getMessage()); |
178 |
|
} |
179 |
2
|
if (!failed) { |
180 |
0
|
fail("too many calls accepted"); |
181 |
|
} |
182 |
|
} |
183 |
|
|
|
|
| 88,9% |
Uncovered Elements: 2 (18) |
Complexity: 3 |
Complexity Density: 0,19 |
1
PASS
|
|
184 |
2
|
@Test... |
185 |
|
public void defaultBehavior() { |
186 |
2
|
reset(mock); |
187 |
|
|
188 |
2
|
expect(mock.booleanReturningMethod(1)).andReturn(true).andReturn(false).andReturn(true); |
189 |
2
|
expect(mock.booleanReturningMethod(anyInt())).andStubReturn(true); |
190 |
|
|
191 |
2
|
replay(mock); |
192 |
|
|
193 |
2
|
assertEquals(true, mock.booleanReturningMethod(2)); |
194 |
2
|
assertEquals(true, mock.booleanReturningMethod(3)); |
195 |
2
|
assertEquals(true, mock.booleanReturningMethod(1)); |
196 |
2
|
assertEquals(false, mock.booleanReturningMethod(1)); |
197 |
2
|
assertEquals(true, mock.booleanReturningMethod(3)); |
198 |
|
|
199 |
2
|
boolean failed = false; |
200 |
2
|
try { |
201 |
2
|
verify(mock); |
202 |
|
} catch (final AssertionError expected) { |
203 |
2
|
failed = true; |
204 |
2
|
assertEquals("\n Expectation failure on verify:" |
205 |
|
+ "\n booleanReturningMethod(1): expected: 3, actual: 2", expected.getMessage()); |
206 |
|
} |
207 |
2
|
if (!failed) { |
208 |
0
|
fail("too few calls accepted"); |
209 |
|
} |
210 |
|
} |
211 |
|
|
|
|
| 84,6% |
Uncovered Elements: 2 (13) |
Complexity: 3 |
Complexity Density: 0,27 |
1
PASS
|
|
212 |
2
|
@Test... |
213 |
|
public void unexpectedCallWithArray() { |
214 |
2
|
reset(mock); |
215 |
2
|
mock.arrayMethod(aryEq(new String[] { "Test", "Test 2" })); |
216 |
2
|
replay(mock); |
217 |
2
|
boolean failed = false; |
218 |
2
|
final String[] strings = new String[] { "Test" }; |
219 |
2
|
try { |
220 |
2
|
mock.arrayMethod(strings); |
221 |
|
} catch (final AssertionError expected) { |
222 |
2
|
failed = true; |
223 |
2
|
assertEquals("\n Unexpected method call arrayMethod(" + "[\"Test\"]" + "):" |
224 |
|
+ "\n arrayMethod([\"Test\", \"Test 2\"]): expected: 1, actual: 0", expected |
225 |
|
.getMessage()); |
226 |
|
} |
227 |
2
|
if (!failed) { |
228 |
0
|
fail("exception expected"); |
229 |
|
} |
230 |
|
|
231 |
|
} |
232 |
|
} |