KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jmock > MockTest


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

3 package test.jmock;
4
5 import junit.framework.AssertionFailedError;
6 import junit.framework.TestCase;
7 import org.jmock.Mock;
8 import org.jmock.core.InvocationMatcher;
9 import org.jmock.core.Invokable;
10 import org.jmock.expectation.AssertMo;
11 import test.jmock.builder.testsupport.MockMatchBuilder;
12 import test.jmock.core.DummyInterface;
13 import test.jmock.core.testsupport.MockDynamicMock;
14 import test.jmock.core.testsupport.MockInvocationMatcher;
15 import test.jmock.core.testsupport.MockInvokable;
16 import test.jmock.core.testsupport.MockStub;
17
18
19 public class MockTest extends TestCase
20 {
21
22     private MockDynamicMock mockCoreMock = new MockDynamicMock();
23     private Mock mock = new Mock(mockCoreMock);
24
25     public void testToStringComesFromUnderlyingDynamicMock() {
26         mockCoreMock.toStringResult = "some string here";
27         assertEquals("Should be same string", "some string here", mock.toString());
28     }
29
30     public void testPassesExplicitNameToCoreMock() {
31         String JavaDoc explicitName = "EXPLICIT NAME";
32
33         assertEquals("should be explicit name", explicitName,
34                      new Mock(DummyInterface.class, explicitName).toString());
35     }
36
37     public void testDelegatgesResetToCoreMock() {
38         mockCoreMock.resetCalls.setExpected(1);
39
40         mock.reset();
41
42         mockCoreMock.verifyExpectations();
43     }
44
45     public void testDelegatesAddInvokableToCoreMock() {
46         Invokable invokable = new MockInvokable();
47
48         mockCoreMock.addInvokableCalls.setExpected(1);
49         mockCoreMock.addInvokable.setExpected(invokable);
50
51         mock.addInvokable(invokable);
52
53         mockCoreMock.verifyExpectations();
54     }
55
56     public void testStubAddsInvocationMockerAndReturnsBuilder() {
57         mockCoreMock.addInvokableCalls.setExpected(1);
58
59         assertNotNull("Should be invokedMethod expectation", mock.stubs());
60         mockCoreMock.verifyExpectations();
61     }
62
63     public void testExpectAddsInvocationMockerAndAddsExpectationAndReturnsBuilder() {
64         InvocationMatcher expectation = new MockInvocationMatcher();
65
66         mockCoreMock.addInvokableCalls.setExpected(1);
67
68         assertNotNull("Should be invokedMethod expectation", mock.expects(expectation));
69         mockCoreMock.verifyExpectations();
70     }
71
72     public void testVerifyCallsUnderlyingMock() {
73         mockCoreMock.verifyCalls.setExpected(1);
74
75         mock.verify();
76
77         mockCoreMock.verifyExpectations();
78     }
79
80     private interface MockedType
81     {
82     }
83
84     public void testReportsTypesMockedByUnderlyingMock() {
85         mockCoreMock.getMockedTypeCalls.setExpected(1);
86         mockCoreMock.getMockedTypeResult = MockedType.class;
87
88         AssertMo.assertSame("mocked types",
89                             MockedType.class, mock.getMockedType());
90     }
91
92     public void testPassesDefaultStubToCoreMock() {
93         MockStub mockDefaultStub = new MockStub();
94
95         mockCoreMock.setDefaultStub.setExpected(mockDefaultStub);
96
97         mock.setDefaultStub(mockDefaultStub);
98
99         mockCoreMock.verifyExpectations();
100     }
101
102
103     static final String JavaDoc BUILDER_ID = "BUILDER-ID";
104
105     public void testStoresExpectationBuildersByID() {
106         MockMatchBuilder builder1 = new MockMatchBuilder();
107         MockMatchBuilder builder2 = new MockMatchBuilder();
108
109         mock.registerUniqueID(BUILDER_ID + 1, builder1);
110         mock.registerUniqueID(BUILDER_ID + 2, builder2);
111
112         assertSame("should be builder1",
113                    builder1, mock.lookupID(BUILDER_ID + 1));
114         assertSame("should be builder2",
115                    builder2, mock.lookupID(BUILDER_ID + 2));
116     }
117
118     public void testFailsOnLookingUpUnregisteredID() {
119         try {
120             mock.lookupID(BUILDER_ID);
121         }
122         catch (AssertionFailedError ex) {
123             assertTrue("error message should contain invalid id",
124                        ex.getMessage().indexOf(BUILDER_ID) >= 0);
125             return;
126         }
127         fail("expected AssertionFailedError");
128     }
129
130     public void testMethodNamesCanBeUsedToNameBuildersInAdditionToUniqueID() {
131         MockMatchBuilder builder1 = new MockMatchBuilder();
132         MockMatchBuilder builder2 = new MockMatchBuilder();
133
134         mock.registerMethodName(BUILDER_ID + 1, builder1);
135         mock.registerMethodName(BUILDER_ID + 2, builder2);
136
137         assertSame("should be builder1",
138                    builder1, mock.lookupID(BUILDER_ID + 1));
139         assertSame("should be builder2",
140                    builder2, mock.lookupID(BUILDER_ID + 2));
141     }
142
143     public void testDuplicateMethodNameOverridesPreviousMapping() {
144         MockMatchBuilder builder1 = new MockMatchBuilder();
145         MockMatchBuilder builder2 = new MockMatchBuilder();
146
147         mock.registerMethodName(BUILDER_ID, builder1);
148         mock.registerMethodName(BUILDER_ID, builder2);
149
150         assertSame("builder2", builder2, mock.lookupID(BUILDER_ID));
151     }
152
153     public void testDuplicateUniqueIDCausesTestFailure() {
154         MockMatchBuilder builder1 = new MockMatchBuilder();
155         MockMatchBuilder builder2 = new MockMatchBuilder();
156
157         mock.registerUniqueID(BUILDER_ID, builder1);
158         try {
159             mock.registerUniqueID(BUILDER_ID, builder2);
160         }
161         catch (AssertionFailedError ex) {
162             AssertMo.assertIncludes("should contain invalid ID",
163                                     BUILDER_ID, ex.getMessage());
164             return;
165         }
166
167         fail("expected failure");
168     }
169 }
170
171
Popular Tags