KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > mockobjects > dynamic > MockTest


1 /*
2  * Created on 04-Apr-2003
3  */

4 package test.mockobjects.dynamic;
5
6 import com.mockobjects.constraint.*;
7 import com.mockobjects.constraint.IsEqual;
8 import com.mockobjects.dynamic.*;
9 import com.mockobjects.util.*;
10
11 import junit.framework.*;
12
13
14 public class MockTest extends TestCase {
15     private static final String JavaDoc MOCK_NAME = "Test mock";
16     final String JavaDoc METHOD_NOARG_NAME = "noArgMethodVoid";
17     final String JavaDoc METHOD_NOARGANDRETURN_NAME = "noArgMethod";
18     final String JavaDoc METHOD_NOARGANDRETURN_RESULT = "resultNoArgs";
19     final String JavaDoc METHOD_ONEARG_NAME = "oneArgMethod";
20     final String JavaDoc METHOD_ONEARG_RESULT = "result1Args";
21     final String JavaDoc METHOD_TWOARG_NAME = "twoArgMethod";
22     final String JavaDoc METHOD_TWOARG_RESULT = "resultTwoArgs";
23     final Throwable JavaDoc METHOD_EXCEPTION = new DummyThrowable("Configured test throwable");
24     final Object JavaDoc[] METHOD_NOARG_ARGS = new Object JavaDoc[0];
25     final String JavaDoc[] METHOD_ONEARG_ARGS = new String JavaDoc[] { "oneP1" };
26     final ConstraintMatcher METHOD_ONEARG_CONSTRAINTS = C.args(C.eq("oneP1"));
27     final String JavaDoc[] METHOD_TWOARG_ARGS = new String JavaDoc[] { "twoP1", "twoP2" };
28     final ConstraintMatcher METHOD_TWOARG_CONSTRAINTS = C.args(C.eq("twoP1"), C.eq("twoP2"));
29     private DummyInterface proxy;
30     private Mock mock;
31     private MockCallFactory mockCallFactory = new MockCallFactory();
32     private MockCallable mockCallMatch = new MockCallable();
33     private MockCallable mockExpectedCall = new MockCallable();
34     private MockCallable mockReturnStub = new MockCallable();
35     private MockCallable mockThrowStub = new MockCallable();
36     private MockCallable mockVoidStub = new MockCallable();
37     private MockCallableAddable mockCallableAddable = new MockCallableAddable();
38
39     public MockTest(String JavaDoc name) throws Exception JavaDoc {
40         super(name);
41     }
42
43     public void setUp() {
44         mock = new Mock(mockCallFactory, mockCallableAddable, DummyInterface.class, MOCK_NAME);
45
46         try {
47             proxy = (DummyInterface)mock.proxy();
48         } catch (ClassCastException JavaDoc ex) {
49             fail("proxy is not of expected interface type");
50         }
51     }
52
53     public void testExpectManyAndVoid() throws Throwable JavaDoc {
54         mockCallFactory.setExpectedCreateVoidStubCalls(1);
55         mockCallFactory.setupCreateVoidStub(mockVoidStub);
56         mockCallFactory.addExpectedCreateCallSignature(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, mockVoidStub);
57         mockCallFactory.setupCreateCallSignature(mockCallMatch);
58         mockCallFactory.addExpectedCreateCallExpectation(mockCallMatch);
59         mockCallFactory.setupCreateCallExpectation(mockExpectedCall);
60
61         mockCallableAddable.addExpectedAddExpect(mockExpectedCall);
62
63         mock.expect(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS);
64
65         Verifier.verifyObject(this);
66         mockCallableAddable.verifyExpectations();
67     }
68     
69     public void testExpectNoneAndReturn() throws Throwable JavaDoc {
70         mockCallFactory.addExpectedCreateReturnStub(METHOD_NOARGANDRETURN_RESULT);
71         mockCallFactory.setupCreateReturnStub(mockReturnStub);
72         mockCallFactory.addExpectedCreateCallSignature(METHOD_NOARGANDRETURN_NAME, C.NO_ARGS, mockReturnStub);
73         mockCallFactory.setupCreateCallSignature(mockCallMatch);
74         mockCallFactory.addExpectedCreateCallExpectation(mockCallMatch);
75         mockCallFactory.setupCreateCallExpectation(mockExpectedCall);
76
77         mockCallableAddable.addExpectedAddExpect(mockExpectedCall);
78
79         mock.expectAndReturn(METHOD_NOARGANDRETURN_NAME, METHOD_NOARGANDRETURN_RESULT);
80
81         Verifier.verifyObject(this);
82         mockCallableAddable.verifyExpectations();
83     }
84     
85     public void testExpectNoneAndThrow() throws Throwable JavaDoc {
86         mockCallFactory.addExpectedCreateThrowStub(METHOD_EXCEPTION);
87         mockCallFactory.setupCreateThrowStub(mockThrowStub);
88         mockCallFactory.addExpectedCreateCallSignature(METHOD_NOARGANDRETURN_NAME, C.NO_ARGS, mockThrowStub);
89         mockCallFactory.setupCreateCallSignature(mockCallMatch);
90         mockCallFactory.addExpectedCreateCallExpectation(mockCallMatch);
91         mockCallFactory.setupCreateCallExpectation(mockExpectedCall);
92
93         mockCallableAddable.addExpectedAddExpect(mockExpectedCall);
94
95         mock.expectAndThrow(METHOD_NOARGANDRETURN_NAME, METHOD_EXCEPTION);
96
97         Verifier.verifyObject(this);
98         mockCallableAddable.verifyExpectations();
99     }
100     
101     public void testExpectOneAndThrow() throws Throwable JavaDoc {
102         mockCallFactory.addExpectedCreateThrowStub(METHOD_EXCEPTION);
103         mockCallFactory.setupCreateThrowStub(mockThrowStub);
104         mockCallFactory.addExpectedCreateCallSignature(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockThrowStub);
105         mockCallFactory.setupCreateCallSignature(mockCallMatch);
106         mockCallFactory.addExpectedCreateCallExpectation(mockCallMatch);
107         mockCallFactory.setupCreateCallExpectation(mockExpectedCall);
108
109         mockCallableAddable.addExpectedAddExpect(mockExpectedCall);
110
111         mock.expectAndThrow(METHOD_ONEARG_NAME, METHOD_ONEARG_ARGS[0], METHOD_EXCEPTION);
112
113         Verifier.verifyObject(this);
114         mockCallableAddable.verifyExpectations();
115     }
116     
117     public void testExpectNoneAndVoid() throws Throwable JavaDoc {
118         mockCallFactory.setExpectedCreateVoidStubCalls(1);
119         mockCallFactory.setupCreateVoidStub(mockVoidStub);
120         mockCallFactory.addExpectedCreateCallSignature(METHOD_NOARG_NAME, C.NO_ARGS, mockVoidStub);
121         mockCallFactory.setupCreateCallSignature(mockCallMatch);
122         mockCallFactory.addExpectedCreateCallExpectation(mockCallMatch);
123         mockCallFactory.setupCreateCallExpectation(mockExpectedCall);
124     
125         mockCallableAddable.addExpectedAddExpect(mockExpectedCall);
126     
127         mock.expect(METHOD_NOARG_NAME);
128     
129         Verifier.verifyObject(this);
130         mockCallableAddable.verifyExpectations();
131     }
132     
133     public void testExpectOneAndVoid() throws Throwable JavaDoc {
134         mockCallFactory.setExpectedCreateVoidStubCalls(1);
135         mockCallFactory.setupCreateVoidStub(mockVoidStub);
136         mockCallFactory.addExpectedCreateCallSignature(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockVoidStub);
137         mockCallFactory.setupCreateCallSignature(mockCallMatch);
138         mockCallFactory.addExpectedCreateCallExpectation(mockCallMatch);
139         mockCallFactory.setupCreateCallExpectation(mockExpectedCall);
140
141         mockCallableAddable.addExpectedAddExpect(mockExpectedCall);
142
143         mock.expect(METHOD_ONEARG_NAME, METHOD_ONEARG_ARGS[0]);
144
145         Verifier.verifyObject(this);
146         mockCallableAddable.verifyExpectations();
147     }
148     
149     public void testExpectWithConstraint() throws Throwable JavaDoc {
150         mockCallFactory.setupCreateVoidStub(mockVoidStub);
151         mockCallFactory.addExpectedCreateCallSignature(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockVoidStub);
152         mockCallFactory.setupCreateCallSignature(mockCallMatch);
153         mockCallFactory.setupCreateCallExpectation(mockExpectedCall);
154
155         mockCallableAddable.addExpectedAddExpect(mockExpectedCall);
156
157         mock.expect(METHOD_ONEARG_NAME, new IsEqual(METHOD_ONEARG_ARGS[0]));
158
159         Verifier.verifyObject(this);
160         mockCallableAddable.verifyExpectations();
161     }
162     
163     public void testExpectWithConstraintArray() throws Throwable JavaDoc {
164         mockCallFactory.setupCreateVoidStub(mockVoidStub);
165         mockCallFactory.addExpectedCreateCallSignature(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockVoidStub);
166         mockCallFactory.setupCreateCallSignature(mockCallMatch);
167         mockCallFactory.setupCreateCallExpectation(mockExpectedCall);
168
169         mockCallableAddable.addExpectedAddExpect(mockExpectedCall);
170
171         mock.expect(METHOD_ONEARG_NAME, new Constraint[] { new IsEqual(METHOD_ONEARG_ARGS[0])});
172
173         Verifier.verifyObject(this);
174         mockCallableAddable.verifyExpectations();
175     }
176
177
178     public void testExpectManyAndReturn() throws Throwable JavaDoc {
179         mockCallFactory.addExpectedCreateReturnStub(METHOD_TWOARG_RESULT);
180         mockCallFactory.setupCreateReturnStub(mockReturnStub);
181         mockCallFactory.addExpectedCreateCallSignature(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, mockReturnStub);
182         mockCallFactory.setupCreateCallSignature(mockCallMatch);
183         mockCallFactory.addExpectedCreateCallExpectation(mockCallMatch);
184         mockCallFactory.setupCreateCallExpectation(mockExpectedCall);
185
186         mockCallableAddable.addExpectedAddExpect(mockExpectedCall);
187         mockCallableAddable.setupCall(METHOD_TWOARG_RESULT);
188
189         mock.expectAndReturn(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, METHOD_TWOARG_RESULT);
190
191         Verifier.verifyObject(this);
192         mockCallableAddable.verifyExpectations();
193     }
194
195     public void testExpectManyAndThrow() throws Throwable JavaDoc {
196         mockCallFactory.addExpectedCreateThrowStub(METHOD_EXCEPTION);
197         mockCallFactory.setupCreateThrowStub(mockThrowStub);
198         mockCallFactory.addExpectedCreateCallSignature(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, mockThrowStub);
199         mockCallFactory.setupCreateCallSignature(mockCallMatch);
200         mockCallFactory.addExpectedCreateCallExpectation(mockCallMatch);
201         mockCallFactory.setupCreateCallExpectation(mockExpectedCall);
202
203         mockCallableAddable.addExpectedAddExpect(mockExpectedCall);
204         mockCallableAddable.setupCall(METHOD_TWOARG_RESULT);
205
206         mock.expectAndThrow(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, METHOD_EXCEPTION);
207
208         Verifier.verifyObject(this);
209         mockCallableAddable.verifyExpectations();
210     }
211
212     public void testExpectOneAndReturn() throws Throwable JavaDoc {
213         mockCallFactory.addExpectedCreateReturnStub(METHOD_ONEARG_RESULT);
214         mockCallFactory.setupCreateReturnStub(mockReturnStub);
215         mockCallFactory.addExpectedCreateCallSignature(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockReturnStub);
216         mockCallFactory.setupCreateCallSignature(mockCallMatch);
217         mockCallFactory.addExpectedCreateCallExpectation(mockCallMatch);
218         mockCallFactory.setupCreateCallExpectation(mockExpectedCall);
219
220         mockCallableAddable.addExpectedAddExpect(mockExpectedCall);
221         mockCallableAddable.setupCall(METHOD_ONEARG_RESULT);
222
223         mock.expectAndReturn(METHOD_ONEARG_NAME, METHOD_ONEARG_ARGS[0], METHOD_ONEARG_RESULT);
224
225         Verifier.verifyObject(this);
226         mockCallableAddable.verifyExpectations();
227     }
228
229     public void testMatchManyAndReturn() throws Throwable JavaDoc {
230         mockCallFactory.addExpectedCreateReturnStub(METHOD_TWOARG_RESULT);
231         mockCallFactory.setupCreateReturnStub(mockReturnStub);
232         mockCallFactory.addExpectedCreateCallSignature(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, mockReturnStub);
233         mockCallFactory.setupCreateCallSignature(mockCallMatch);
234
235         mockCallableAddable.addExpectedAddMatch(mockCallMatch);
236         mockCallableAddable.setupCall(METHOD_TWOARG_RESULT);
237
238         mock.matchAndReturn(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, METHOD_TWOARG_RESULT);
239
240         Verifier.verifyObject(this);
241         mockCallableAddable.verifyExpectations();
242     }
243
244     public void testMatchNoneAndThrow() throws Throwable JavaDoc {
245         mockCallFactory.addExpectedCreateThrowStub(METHOD_EXCEPTION);
246         mockCallFactory.setupCreateThrowStub(mockThrowStub);
247         mockCallFactory.addExpectedCreateCallSignature(METHOD_NOARG_NAME, C.NO_ARGS, mockThrowStub);
248         mockCallFactory.setupCreateCallSignature(mockCallMatch);
249
250         mockCallableAddable.addExpectedAddMatch(mockCallMatch);
251         mockCallableAddable.setupCall(METHOD_NOARGANDRETURN_RESULT);
252
253         mock.matchAndThrow(METHOD_NOARG_NAME, METHOD_EXCEPTION);
254
255         Verifier.verifyObject(this);
256         mockCallableAddable.verifyExpectations();
257     }
258     
259     public void testMatchOneAndThrow() throws Throwable JavaDoc {
260         mockCallFactory.addExpectedCreateThrowStub(METHOD_EXCEPTION);
261         mockCallFactory.setupCreateThrowStub(mockThrowStub);
262         mockCallFactory.addExpectedCreateCallSignature(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockThrowStub);
263         mockCallFactory.setupCreateCallSignature(mockCallMatch);
264
265         mockCallableAddable.addExpectedAddMatch(mockCallMatch);
266         mockCallableAddable.setupCall(METHOD_ONEARG_RESULT);
267
268         mock.matchAndThrow(METHOD_ONEARG_NAME, METHOD_ONEARG_ARGS[0], METHOD_EXCEPTION);
269
270         Verifier.verifyObject(this);
271         mockCallableAddable.verifyExpectations();
272     }
273     
274     public void testMatchManyAndThrow() throws Throwable JavaDoc {
275         mockCallFactory.addExpectedCreateThrowStub(METHOD_EXCEPTION);
276         mockCallFactory.setupCreateThrowStub(mockThrowStub);
277         mockCallFactory.addExpectedCreateCallSignature(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, mockThrowStub);
278         mockCallFactory.setupCreateCallSignature(mockCallMatch);
279
280         mockCallableAddable.addExpectedAddMatch(mockCallMatch);
281         mockCallableAddable.setupCall(METHOD_TWOARG_RESULT);
282
283         mock.matchAndThrow(METHOD_TWOARG_NAME, METHOD_TWOARG_CONSTRAINTS, METHOD_EXCEPTION);
284
285         Verifier.verifyObject(this);
286         mockCallableAddable.verifyExpectations();
287     }
288
289     public void testMatchOneAndReturn() throws Throwable JavaDoc {
290         mockCallFactory.addExpectedCreateReturnStub(METHOD_ONEARG_RESULT);
291         mockCallFactory.setupCreateReturnStub(mockReturnStub);
292         mockCallFactory.addExpectedCreateCallSignature(METHOD_ONEARG_NAME, METHOD_ONEARG_CONSTRAINTS, mockReturnStub);
293         mockCallFactory.setupCreateCallSignature(mockCallMatch);
294
295         mockCallableAddable.addExpectedAddMatch(mockCallMatch);
296         mockCallableAddable.setupCall(METHOD_ONEARG_RESULT);
297
298         mock.matchAndReturn(METHOD_ONEARG_NAME, METHOD_ONEARG_ARGS[0], METHOD_ONEARG_RESULT);
299
300         Verifier.verifyObject(this);
301         mockCallableAddable.verifyExpectations();
302     }
303     
304     public void testMatchNoneAndReturn() throws Throwable JavaDoc {
305         mockCallFactory.addExpectedCreateReturnStub(METHOD_NOARGANDRETURN_RESULT);
306         mockCallFactory.setupCreateReturnStub(mockReturnStub);
307         mockCallFactory.addExpectedCreateCallSignature(METHOD_NOARG_NAME, C.NO_ARGS, mockReturnStub);
308         mockCallFactory.setupCreateCallSignature(mockCallMatch);
309
310         mockCallableAddable.addExpectedAddMatch(mockCallMatch);
311         mockCallableAddable.setupCall(METHOD_ONEARG_RESULT);
312
313         mock.matchAndReturn(METHOD_NOARG_NAME, METHOD_NOARGANDRETURN_RESULT);
314
315         Verifier.verifyObject(this);
316         mockCallableAddable.verifyExpectations();
317     }
318
319     public void testMockAnnotatesAssertionFailedErrors()
320         throws Throwable JavaDoc {
321         final String JavaDoc originalMessage = "original message";
322
323         mockCallableAddable.setupCall(new AssertionFailedError(originalMessage));
324
325         try {
326             proxy.noArgMethodVoid();
327         } catch (AssertionFailedError err) {
328             AssertMo.assertIncludes("should contain original message", originalMessage, err.getMessage());
329             AssertMo.assertIncludes("should contain mock name", MOCK_NAME, err.getMessage());
330         }
331     }
332
333     public void testMockNameFromClass() throws Exception JavaDoc {
334         assertEquals("mockString", Mock.mockNameFromClass(String JavaDoc.class));
335     }
336
337     public void testMockProxyReturnsConfiguredResult()
338         throws Throwable JavaDoc {
339         final String JavaDoc result = "configured result";
340
341         mockCallableAddable.setupCall(result);
342
343         assertSame("result is returned by mock", result, proxy.oneArgMethod(METHOD_TWOARG_ARGS[0]));
344     }
345
346     public void testMockProxySendsAllArgument() throws Throwable JavaDoc {
347         mockCallableAddable.addExpectedCall(mock, METHOD_TWOARG_NAME, METHOD_TWOARG_ARGS);
348         mockCallableAddable.setupCall("result ignored");
349
350         proxy.twoArgMethod(METHOD_TWOARG_ARGS[0], METHOD_TWOARG_ARGS[1]);
351
352         mockCallableAddable.verifyExpectations();
353     }
354
355     public void testMockProxySendsEmptyArrayWhenNoArguments()
356         throws Exception JavaDoc {
357         mockCallableAddable.addExpectedCall(mock, METHOD_NOARG_NAME, METHOD_NOARG_ARGS);
358         mockCallableAddable.setupCall("result ignored");
359
360         proxy.noArgMethodVoid();
361
362         Verifier.verifyObject(this);
363         mockCallableAddable.verifyExpectations();
364     }
365
366     public void testMockProxyThrowsConfiguredExceptions()
367         throws Throwable JavaDoc {
368         final Throwable JavaDoc throwable = new DummyThrowable();
369
370         mockCallableAddable.setupCall(new ThrowStub(throwable));
371
372         try {
373             proxy.noArgMethodVoid();
374         } catch (Throwable JavaDoc ex) {
375             assertSame("exception is caught by mock", throwable, ex);
376         }
377     }
378
379     public void testMockToStringContainsName() {
380         AssertMo.assertIncludes("result of toString() should include name", MOCK_NAME, mock.toString());
381     }
382
383     public void testMockVerifies() throws Exception JavaDoc {
384         mockCallableAddable.setExpectedVerifyCalls(1);
385
386         mock.verify();
387
388         mockCallableAddable.verifyExpectations();
389     }
390     
391     public void testProxyEquality() throws Exception JavaDoc {
392         assertTrue("Should handle proxy equality without checking expectations", proxy.equals(proxy));
393     }
394     
395     public void testProxyInEquality() throws Exception JavaDoc {
396         boolean IGNORED_RESULT = true;
397         CallStub ret = new ReturnStub(new Boolean JavaDoc(IGNORED_RESULT));
398         mockCallFactory.setupCreateReturnStub(ret);
399         mockCallFactory.setupCreateCallSignature(new CallSignature("call",C.anyArgs(1),ret));
400         mockCallableAddable.setupCall(new Boolean JavaDoc(false));
401         
402         mock.matchAndReturn("call", C.anyArgs(1), IGNORED_RESULT);
403         assertFalse("Should handle proxy inequality by calling through", proxy.equals("not a proxy"));
404         
405         Verifier.verifyObject(this);
406     }
407     
408     public void testProxyToString() throws Exception JavaDoc {
409             assertEquals("Should get a mock name without touching the underlying mock", MOCK_NAME, DynamicUtil.proxyToString(proxy));
410             mock.verify(); // should not fail on a proxyToString call
411
}
412 }
413
Popular Tags