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 java.lang.reflect.Field; |
23 |
|
import java.util.ArrayList; |
24 |
|
|
25 |
|
import org.easymock.ConstructorArgs; |
26 |
|
import org.easymock.IMocksControl; |
27 |
|
import org.easymock.internal.ClassExtensionHelper; |
28 |
|
import org.easymock.internal.MockBuilder; |
29 |
|
import org.junit.Before; |
30 |
|
import org.junit.Test; |
31 |
|
|
32 |
|
|
33 |
|
@author |
34 |
|
|
|
|
| 91,2% |
Uncovered Elements: 11 (125) |
Complexity: 35 |
Complexity Density: 0,35 |
|
35 |
|
public class MockBuilderTest { |
36 |
|
|
37 |
|
private MockBuilder<ArrayList<String>> builder; |
38 |
|
|
39 |
|
private ArrayList<String> mock; |
40 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
41 |
46
|
@SuppressWarnings("unchecked")... |
42 |
|
@Before |
43 |
|
public void setUp() throws Exception { |
44 |
46
|
builder = new MockBuilder(ArrayList.class); |
45 |
|
} |
46 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 1 |
Complexity Density: 0,06 |
1
PASS
|
|
47 |
2
|
@Test... |
48 |
|
public void testAddMockedMethod() throws NoSuchMethodException { |
49 |
2
|
builder.addMockedMethod(ArrayList.class.getMethod("size")).addMockedMethod("contains") |
50 |
|
.addMockedMethod("add", Object.class).addMockedMethods("clear", "isEmpty").addMockedMethods( |
51 |
|
ArrayList.class.getMethod("get", int.class), |
52 |
|
ArrayList.class.getMethod("indexOf", Object.class)); |
53 |
|
|
54 |
2
|
mock = builder.createMock(); |
55 |
|
|
56 |
2
|
expect(mock.size()).andReturn(3); |
57 |
2
|
expect(mock.contains("test")).andReturn(true); |
58 |
2
|
expect(mock.add("added")).andReturn(true); |
59 |
2
|
mock.clear(); |
60 |
2
|
expect(mock.isEmpty()).andReturn(false); |
61 |
2
|
expect(mock.get(1)).andReturn("result"); |
62 |
2
|
expect(mock.indexOf("t")).andReturn(2); |
63 |
|
|
64 |
2
|
replay(mock); |
65 |
|
|
66 |
2
|
assertEquals(3, mock.size()); |
67 |
2
|
assertEquals(true, mock.contains("test")); |
68 |
2
|
assertEquals(true, mock.add("added")); |
69 |
2
|
mock.clear(); |
70 |
2
|
assertEquals(false, mock.isEmpty()); |
71 |
2
|
assertEquals("result", mock.get(1)); |
72 |
2
|
assertEquals(2, mock.indexOf("t")); |
73 |
|
|
74 |
2
|
verify(mock); |
75 |
|
} |
76 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
77 |
2
|
@Test(expected = IllegalArgumentException.class)... |
78 |
|
public void testAddMethod_NotExisting() { |
79 |
2
|
builder.addMockedMethod(".."); |
80 |
|
} |
81 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
82 |
2
|
@Test(expected = IllegalArgumentException.class)... |
83 |
|
public void testAddMethodWithParams_NotExisting() { |
84 |
2
|
builder.addMockedMethod("..", String.class); |
85 |
|
} |
86 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0,5 |
1
PASS
|
|
87 |
2
|
@Test... |
88 |
|
public void testWithConstructorParams() { |
89 |
2
|
builder.withConstructor(int.class).withArgs(-3); |
90 |
2
|
try { |
91 |
2
|
builder.createMock(); |
92 |
0
|
fail("instantiation should fail because of negative"); |
93 |
|
} catch (final RuntimeException e) { |
94 |
|
} |
95 |
|
} |
96 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
97 |
2
|
@Test(expected = IllegalArgumentException.class)... |
98 |
|
public void testWithConstructor_WrongClass() { |
99 |
2
|
builder.withConstructor(long.class); |
100 |
|
} |
101 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0,17 |
1
PASS
|
|
102 |
2
|
@Test... |
103 |
|
public void testWithEmptyConstructor() throws Exception { |
104 |
2
|
mock = builder.withConstructor().createMock(); |
105 |
2
|
final Field field = ArrayList.class.getDeclaredField("elementData"); |
106 |
2
|
field.setAccessible(true); |
107 |
2
|
final int expected = ((Object[]) field.get(new ArrayList<String>())).length; |
108 |
2
|
final int actual = ((Object[]) field.get(mock)).length; |
109 |
2
|
assertEquals(expected, actual); |
110 |
|
} |
111 |
|
|
|
|
| 66,7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0,67 |
1
PASS
|
|
112 |
2
|
@Test... |
113 |
|
public void testWithEmptyConstructor_NoEmptyConstructor() throws Exception { |
114 |
2
|
try { |
115 |
2
|
createMockBuilder(Integer.class).withConstructor().createMock(); |
116 |
0
|
fail("no empty constructor should be found"); |
117 |
|
} catch (final IllegalArgumentException e) { |
118 |
|
} |
119 |
|
} |
120 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0,5 |
1
PASS
|
|
121 |
2
|
@Test... |
122 |
|
public void testWithConstructor() throws NoSuchMethodException { |
123 |
2
|
builder.withConstructor(ArrayList.class.getConstructor(int.class)).withArgs(-3); |
124 |
2
|
try { |
125 |
2
|
builder.createMock(); |
126 |
0
|
fail("instantiation should fail because of negative"); |
127 |
|
} catch (final RuntimeException e) { |
128 |
|
} |
129 |
|
} |
130 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
131 |
2
|
@Test(expected = IllegalStateException.class)... |
132 |
|
public void testWithConstructor_Twice() { |
133 |
2
|
builder.withConstructor(int.class).withConstructor(int.class); |
134 |
|
} |
135 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0,4 |
1
PASS
|
|
136 |
2
|
@Test... |
137 |
|
public void testWithConstructorConstructorArgs() throws NoSuchMethodException { |
138 |
2
|
final ConstructorArgs args = new ConstructorArgs(ArrayList.class.getConstructor(int.class), Integer |
139 |
|
.valueOf(-3)); |
140 |
2
|
builder.withConstructor(args); |
141 |
2
|
try { |
142 |
2
|
builder.createMock(); |
143 |
0
|
fail("instantiation should fail because of negative"); |
144 |
|
} catch (final RuntimeException e) { |
145 |
|
} |
146 |
|
} |
147 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0,5 |
1
PASS
|
|
148 |
2
|
@Test... |
149 |
|
public void testWithConstructorWithArgs() throws NoSuchMethodException { |
150 |
2
|
builder.withConstructor(-3); |
151 |
2
|
try { |
152 |
2
|
builder.createMock(); |
153 |
0
|
fail("instantiation should fail because of negative"); |
154 |
|
} catch (final RuntimeException e) { |
155 |
|
} |
156 |
|
} |
157 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
158 |
2
|
@Test(expected = IllegalArgumentException.class)... |
159 |
|
public void testWithConstructorWithArgs_NotExisting() throws NoSuchMethodException { |
160 |
2
|
builder.withConstructor("string"); |
161 |
|
} |
162 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0,5 |
1
PASS
|
|
163 |
2
|
@Test... |
164 |
|
public void testWithArgsTwice() { |
165 |
2
|
try { |
166 |
2
|
builder.withConstructor(int.class).withArgs(3).withArgs(2); |
167 |
0
|
fail("withArgs called twice"); |
168 |
|
} catch (final IllegalStateException e) { |
169 |
2
|
assertEquals("Trying to define the constructor arguments more than once.", e.getMessage()); |
170 |
|
} |
171 |
|
} |
172 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0,5 |
1
PASS
|
|
173 |
2
|
@Test... |
174 |
|
public void testWithArgs_WithoutConstructor() { |
175 |
2
|
try { |
176 |
2
|
builder.withArgs(2); |
177 |
0
|
fail("withArgs without constructor"); |
178 |
|
} catch (final IllegalStateException e) { |
179 |
2
|
assertEquals("Trying to define constructor arguments without first setting their type.", e |
180 |
|
.getMessage()); |
181 |
|
} |
182 |
|
} |
183 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
1
PASS
|
|
184 |
2
|
@Test... |
185 |
|
public void testCreateMockIMocksControl() { |
186 |
2
|
final IMocksControl ctrl = createControl(); |
187 |
2
|
mock = builder.createMock(ctrl); |
188 |
2
|
assertSame(ClassExtensionHelper.getControl(mock), ctrl); |
189 |
|
} |
190 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0,4 |
1
PASS
|
|
191 |
2
|
@Test... |
192 |
|
public void testCreateMock() { |
193 |
2
|
mock = builder.addMockedMethod("size").addMockedMethod("toString").createMock(); |
194 |
2
|
replay(mock); |
195 |
2
|
try { |
196 |
2
|
mock.size(); |
197 |
0
|
fail("Unexpected call"); |
198 |
|
} catch (final AssertionError e) { |
199 |
|
} |
200 |
|
} |
201 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0,25 |
1
PASS
|
|
202 |
2
|
@Test... |
203 |
|
public void testCreateNiceMock() { |
204 |
2
|
mock = builder.addMockedMethod("size").addMockedMethod("toString").createNiceMock(); |
205 |
2
|
replay(mock); |
206 |
2
|
assertEquals(0, mock.size()); |
207 |
2
|
verify(mock); |
208 |
|
} |
209 |
|
|
|
|
| 85,7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0,29 |
1
PASS
|
|
210 |
2
|
@Test... |
211 |
|
public void testCreateStrictMock() { |
212 |
2
|
mock = builder.addMockedMethod("size").addMockedMethod("clear").addMockedMethod("toString") |
213 |
|
.createStrictMock(); |
214 |
2
|
expect(mock.size()).andReturn(1); |
215 |
2
|
mock.clear(); |
216 |
2
|
replay(mock); |
217 |
2
|
try { |
218 |
2
|
mock.clear(); |
219 |
0
|
fail("Unexpected call"); |
220 |
|
} catch (final AssertionError e) { |
221 |
|
} |
222 |
|
} |
223 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0,25 |
1
PASS
|
|
224 |
2
|
@Test... |
225 |
|
public void testCreateMockStringIMocksControl() { |
226 |
2
|
final IMocksControl ctrl = createControl(); |
227 |
2
|
mock = builder.addMockedMethod("toString").createMock("myName", ctrl); |
228 |
2
|
assertSame(ClassExtensionHelper.getControl(mock), ctrl); |
229 |
2
|
assertTrue(mock.toString().contains("myName")); |
230 |
|
} |
231 |
|
|
|
|
| 83,3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0,33 |
1
PASS
|
|
232 |
2
|
@Test... |
233 |
|
public void testCreateMockString() { |
234 |
2
|
mock = builder.addMockedMethod("size").addMockedMethod("toString").createMock("myName"); |
235 |
2
|
replay(mock); |
236 |
2
|
try { |
237 |
2
|
mock.size(); |
238 |
0
|
fail("Unexpected call"); |
239 |
|
} catch (final AssertionError e) { |
240 |
2
|
assertTrue(e.getMessage().contains("myName")); |
241 |
|
} |
242 |
|
} |
243 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0,2 |
1
PASS
|
|
244 |
2
|
@Test... |
245 |
|
public void testCreateNiceMockString() { |
246 |
2
|
mock = builder.addMockedMethod("size").addMockedMethod("toString").createNiceMock("myName"); |
247 |
2
|
replay(mock); |
248 |
2
|
assertEquals(0, mock.size()); |
249 |
2
|
verify(mock); |
250 |
2
|
assertTrue(mock.toString().contains("myName")); |
251 |
|
} |
252 |
|
|
|
|
| 87,5% |
Uncovered Elements: 1 (8) |
Complexity: 2 |
Complexity Density: 0,25 |
1
PASS
|
|
253 |
2
|
@Test... |
254 |
|
public void testCreateStrictMockString() throws Throwable { |
255 |
2
|
mock = builder.addMockedMethod("size").addMockedMethod("clear").addMockedMethod("toString") |
256 |
|
.createStrictMock("myName"); |
257 |
2
|
expect(mock.size()).andReturn(1); |
258 |
2
|
mock.clear(); |
259 |
2
|
replay(mock); |
260 |
2
|
try { |
261 |
2
|
mock.clear(); |
262 |
0
|
fail("Unexpected call"); |
263 |
|
} catch (final AssertionError e) { |
264 |
2
|
assertTrue(e.getMessage().contains("myName")); |
265 |
|
} |
266 |
|
} |
267 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
268 |
2
|
@Test(expected = IllegalStateException.class)... |
269 |
|
public void testCreateMock_ConstructorWithoutArgs() { |
270 |
2
|
builder.withConstructor(int.class).createMock(); |
271 |
|
} |
272 |
|
} |