KickJava   Java API By Example, From Geeks To Geeks.

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


1 package test.mockobjects.dynamic;
2
3 import com.mockobjects.dynamic.*;
4 import com.mockobjects.util.*;
5
6 import junit.framework.*;
7
8
9 public class CallOnceExpectationTest extends TestCase {
10
11     final String JavaDoc RESULT = "result!";
12     final String JavaDoc METHOD_NAME = "methodName";
13     final Object JavaDoc[] ARGS = { "arg1", "arg2" };
14     final String JavaDoc DECORATED_DESCRIPTION = DynamicUtil.methodToString( METHOD_NAME, ARGS );
15     
16     Mock ignoredMock = null;
17     MockCallable mockCallable = new MockCallable();
18     CallOnceExpectation call = new CallOnceExpectation( mockCallable );
19     
20     public CallOnceExpectationTest(String JavaDoc name) {
21         super(name);
22     }
23
24     public void setUp() {
25         mockCallable.setupGetDescription(DECORATED_DESCRIPTION);
26     }
27     
28     public void testDescription() {
29         AssertMo.assertIncludes( "should contain decorated's description",
30                                  DECORATED_DESCRIPTION, call.getDescription() );
31         AssertMo.assertIncludes( "should say that decorated call has called status",
32                                  "called", call.getDescription() );
33     }
34     
35     public void testDoesNotVerifyIfNotCalled() {
36         try {
37             call.verify();
38         }
39         catch( AssertionFailedError ex ) {
40             AssertMo.assertIncludes( "should include description of expected call",
41                                      DECORATED_DESCRIPTION, ex.getMessage() );
42             return;
43         }
44         
45         fail( "verify did not fail when expected call not called");
46     }
47     
48     public void testVerifiesIfCalled() throws Throwable JavaDoc {
49         mockCallable.setupCallReturn( RESULT );
50         mockCallable.setExpectedVerifyCalls(1);
51         
52         call.call( ignoredMock, METHOD_NAME, ARGS );
53         call.verify();
54         
55         mockCallable.verifyExpectations();
56     }
57     
58     public void testMatchesDelegated() throws Throwable JavaDoc {
59         mockCallable.setExpectedMatches( METHOD_NAME, ARGS );
60         mockCallable.setupMatchesReturn(true);
61         assertTrue( "returns matches to be true", call.matches( METHOD_NAME, ARGS ) );
62         mockCallable.verifyExpectations();
63     }
64     
65     public void testCallArgumentsPassedThrough() throws Throwable JavaDoc {
66         mockCallable.setExpectedCall(ignoredMock, METHOD_NAME, ARGS);
67         mockCallable.setupCallReturn(RESULT);
68         
69         call.call( ignoredMock, METHOD_NAME, ARGS );
70         mockCallable.verifyExpectations();
71     }
72     
73     public void testDoesNotMatchAfterMethodCalled() throws Throwable JavaDoc
74     {
75         mockCallable.setupMatchesReturn(true);
76         mockCallable.setupCallReturn(RESULT);
77         
78         assertTrue( "First time should match", call.matches(METHOD_NAME, ARGS));
79         call.call(ignoredMock, METHOD_NAME, ARGS);
80         assertFalse( "Second time should not match", call.matches(METHOD_NAME, ARGS));
81     }
82     
83     public void testDecoratedResultPassedThrough() throws Throwable JavaDoc {
84         mockCallable.setupCallReturn(RESULT);
85         
86         assertSame( "should return decorated's result",
87                     RESULT, call.call( ignoredMock, METHOD_NAME, ARGS ) );
88         mockCallable.verifyExpectations();
89     }
90
91     
92     public void testDecoratedExceptionPassedThrough() throws Throwable JavaDoc {
93         final Throwable JavaDoc exception = new DummyThrowable();
94         
95         mockCallable.setupCallThrow(exception);
96         
97         try {
98             call.call( ignoredMock, METHOD_NAME, ARGS );
99             fail("expected decorated's throwable to be thrown");
100         }
101         catch( DummyThrowable ex ) {
102             // expected
103
}
104         
105         mockCallable.verifyExpectations();
106     }
107 }
108
Popular Tags