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