KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jmock > core > AbstractDynamicMockTest


1 /* Copyright (c) 2000-2004 jMock.org
2  */

3 package test.jmock.core;
4
5 import junit.framework.AssertionFailedError;
6 import junit.framework.TestCase;
7 import org.jmock.core.*;
8 import org.jmock.expectation.AssertMo;
9 import test.jmock.core.testsupport.MockInvocationDispatcher;
10 import test.jmock.core.testsupport.MockInvokable;
11 import test.jmock.core.testsupport.MockStub;
12
13
14 public abstract class AbstractDynamicMockTest extends TestCase
15 {
16     private static final String JavaDoc MOCK_NAME = "Test coreMock";
17
18     private DummyInterface proxy;
19     private DynamicMock coreMock;
20     private MockInvocationDispatcher mockDispatcher = new MockInvocationDispatcher();
21     private MockInvokable mockInvokable = new MockInvokable();
22
23     public void setUp() {
24         coreMock = createDynamicMock( MOCK_NAME, mockDispatcher );
25
26         try {
27             proxy = (DummyInterface)coreMock.proxy();
28         }
29         catch (ClassCastException JavaDoc ex) {
30             fail("proxy is not of expected interface type");
31         }
32     }
33
34     protected abstract DynamicMock createDynamicMock( String JavaDoc name, InvocationDispatcher dispatcher );
35     protected abstract Class JavaDoc mockedType();
36
37     protected DummyInterface proxy() {
38         return proxy;
39     }
40
41     public void testReportsMockedType() {
42         assertSame("mocked type",
43                    mockedType(), coreMock.getMockedType());
44     }
45
46     public void testMockAnnotatesAssertionFailedError()
47             throws Throwable JavaDoc {
48         final String JavaDoc originalMessage = "original message";
49
50         Throwable JavaDoc throwable = new AssertionFailedError(originalMessage);
51         mockDispatcher.dispatchThrowable = throwable;
52
53         try {
54             proxy.noArgVoidMethod();
55             fail("should throw AssertionFailedError");
56         }
57         catch (AssertionFailedError err) {
58             AssertMo.assertIncludes("should contain original message", originalMessage, err.getMessage());
59             AssertMo.assertIncludes("should contain coreMock name", MOCK_NAME, err.getMessage());
60         }
61     }
62
63     public void testProxyReturnsConfiguredResult() throws Throwable JavaDoc {
64         final String JavaDoc RESULT = "configured result";
65
66         mockDispatcher.dispatchResult = RESULT;
67
68         assertSame("result is returned by coreMock", RESULT, proxy.oneArgMethod("arg"));
69     }
70
71     public void testExceptionsPropagatedThroughProxy() throws Throwable JavaDoc {
72         final Throwable JavaDoc throwable = new DummyThrowable();
73
74         mockDispatcher.dispatchThrowable = throwable;
75
76         try {
77             proxy.noArgVoidMethod();
78         }
79         catch (Throwable JavaDoc ex) {
80             assertSame("exception is caught by coreMock", throwable, ex);
81             return;
82         }
83         fail("Should have thrown exception");
84     }
85
86     public void testMockVerifies() throws Exception JavaDoc {
87         mockDispatcher.verifyCalls.setExpected(1);
88
89         coreMock.verify();
90
91         // Can't use Verifier as we are verifying "verify"
92
mockDispatcher.verifyExpectations();
93     }
94
95     public void testTestsEqualityForProxy() throws Exception JavaDoc {
96         coreMock = createDynamicMock( "coreMock", new LIFOInvocationDispatcher());
97         proxy = (DummyInterface)coreMock.proxy();
98
99         assertTrue("should be equal", proxy.equals(proxy));
100         assertFalse("should not be equal", proxy.equals(new Object JavaDoc()));
101         assertFalse("shuold not be equal to null", proxy.equals(null));
102     }
103
104     public void testCanOverrideEqualsForProxyBySettingAStub() throws Exception JavaDoc {
105         mockDispatcher.dispatchResult = new Boolean JavaDoc(false);
106
107         mockDispatcher.dispatchInvocation.setExpected(new Invocation(proxy,
108                                                                      Object JavaDoc.class.getMethod("equals", new Class JavaDoc[]{Object JavaDoc.class}),
109                                                                      new Object JavaDoc[]{"not a proxy"}));
110
111         assertFalse("Passes invocation of equals to dispatcher",
112                     proxy.equals("not a proxy"));
113
114         mockDispatcher.verifyExpectations();
115     }
116
117     public void testCalculatesHashCodeForProxy() throws Exception JavaDoc {
118         coreMock = new CoreMock(DummyInterface.class, "coreMock");
119
120         proxy = (DummyInterface)coreMock.proxy();
121
122         assertEquals("same hash code", proxy.hashCode(), proxy.hashCode());
123     }
124
125     public void testCanOverrideHashCodeForProxyBySettingAStub() throws Exception JavaDoc {
126         int expectedHashCode = 1;
127
128         mockDispatcher.dispatchResult = new Integer JavaDoc(expectedHashCode);
129         mockDispatcher.dispatchInvocation.setExpected(new Invocation(proxy, Object JavaDoc.class.getMethod("hashCode", new Class JavaDoc[0]),
130                                                                      new Object JavaDoc[0]));
131
132         assertEquals("proxy hashCode", expectedHashCode, proxy.hashCode());
133
134         mockDispatcher.verifyExpectations();
135     }
136
137     public void testGeneratesMockNameFromInterfaceNameIfNoNameSpecified() throws Exception JavaDoc {
138         assertEquals("mockString", CoreMock.mockNameFromClass(String JavaDoc.class));
139     }
140
141     public void testReturnsNameFromToString() {
142         AssertMo.assertIncludes("result of toString() should include name",
143                                 MOCK_NAME, coreMock.toString());
144     }
145
146     public void testAddsInvokablesToDispatcher() {
147         mockDispatcher.addInvokable.setExpected(mockInvokable);
148
149         coreMock.addInvokable(mockInvokable);
150
151         mockDispatcher.verifyExpectations();
152     }
153
154     public void testExposesDefaultStubOfDispatcher() {
155         MockStub dummyStub = new MockStub("dummyStub");
156
157         mockDispatcher.setDefaultStub.setExpected(dummyStub);
158
159         coreMock.setDefaultStub(dummyStub);
160
161         mockDispatcher.verifyExpectations();
162     }
163
164     public void testResetsDispatcher() {
165         mockDispatcher.clearCalls.setExpected(1);
166
167         coreMock.reset();
168
169         mockDispatcher.verifyExpectations();
170     }
171
172     public void testVerifyFailuresIncludeMockName() {
173         mockDispatcher.verifyFailure = new AssertionFailedError("verify failure");
174
175         mockDispatcher.verifyCalls.setExpected(1);
176
177         try {
178             coreMock.verify();
179         }
180         catch (AssertionFailedError expected) {
181             AssertMo.assertIncludes("Should include mock name", MOCK_NAME, expected.getMessage());
182             mockDispatcher.verifyExpectations();
183             return;
184         }
185         fail("Should have thrown exception");
186     }
187 }
188
Popular Tags