KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > atest > jmock > OrderedInvocationsAcceptanceTest


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

3 package atest.jmock;
4
5 import junit.framework.AssertionFailedError;
6 import org.jmock.Mock;
7 import org.jmock.MockObjectTestCase;
8 import org.jmock.core.DynamicMockError;
9 import org.jmock.expectation.AssertMo;
10
11
12 public class OrderedInvocationsAcceptanceTest
13         extends MockObjectTestCase
14 {
15     private Mock mock;
16     private ExampleInterface proxy;
17
18     public static interface ExampleInterface
19     {
20         void hello();
21
22         void goodbye();
23
24         void moreTeaVicar();
25
26         int count();
27     }
28
29     public void setUp() {
30         mock = mock(ExampleInterface.class, "mock");
31         proxy = (ExampleInterface)mock.proxy();
32     }
33
34     public void testOrderedCallsCanOccurInOrder() {
35         mock.stubs().method("hello").id("hello call");
36         mock.stubs().method("goodbye").after("hello call");
37
38         proxy.hello();
39         proxy.goodbye();
40     }
41
42     public void testOrderedCallsMustNotOccurOutOfOrder() {
43         String JavaDoc priorCall = "HELLO-CALL-ID";
44
45         mock.stubs().method("hello").id(priorCall);
46         mock.stubs().method("goodbye").after(priorCall);
47
48         try {
49             proxy.goodbye();
50             fail("should have thrown DynamicMockError");
51         }
52         catch (DynamicMockError ex) {
53             assertTrue("error message should contain id of prior call",
54                        ex.getMessage().indexOf(priorCall) >= 0);
55         }
56     }
57
58     public void testOrderingDoesNotAffectUnrelatedCalls() {
59         mock.stubs().method("hello").id("hello call");
60         mock.stubs().method("goodbye").after("hello call");
61         mock.stubs().method("moreTeaVicar");
62
63         proxy.hello();
64         proxy.moreTeaVicar();
65         proxy.goodbye();
66     }
67
68     public void testOrderingConstraintsDoNotImplyExpectedCall() {
69         mock.stubs().method("hello").isVoid().id("hello call");
70         mock.stubs().method("goodbye").after("hello call");
71     }
72
73     public void testCanUseMethodNameAsDefaultInvocationID() {
74         mock.stubs().method("hello").isVoid();
75         mock.stubs().method("goodbye").after("hello"); // should not throw error
76
}
77
78     public void testUsingSameMethodNameAsParameterToAfterIsAnError() {
79         mock.stubs().method("count").will(returnValue(1));
80         try {
81             mock.stubs().method("count").after("count").will(returnValue(2));
82         }
83         catch (AssertionFailedError ex) {
84             AssertMo.assertIncludes("should include repeated invokedMethod name",
85                                     "count", ex.getMessage());
86             return;
87         }
88         fail("should have failed");
89     }
90
91     public void testCanSpecifyOrderOverDifferentMocks() {
92         Mock otherMock = mock(ExampleInterface.class, "otherMock");
93         ExampleInterface otherProxy = (ExampleInterface)otherMock.proxy();
94
95         otherMock.stubs().method("hello").isVoid();
96
97         mock.stubs().method("goodbye").after(otherMock, "hello");
98
99         otherProxy.hello();
100         proxy.goodbye();
101     }
102
103     public void testDetectsUnexpectedOrderOverDifferentMocks() {
104         String JavaDoc otherMockName = "otherMock";
105         String JavaDoc priorCall = "HELLO-CALL-ID";
106         Mock otherMock = mock(ExampleInterface.class, otherMockName);
107
108         otherMock.stubs().method("hello").id(priorCall);
109         mock.stubs().method("goodbye").after(otherMock, priorCall);
110
111         try {
112             proxy.goodbye();
113             fail("expected DynamicMockError");
114         }
115         catch (DynamicMockError ex) {
116             assertTrue("error message should contain id of prior call",
117                        ex.getMessage().indexOf(priorCall) >= 0);
118             assertTrue("error message should contain name of mock receiving prior call",
119                        ex.getMessage().indexOf(otherMockName) >= 0);
120
121         }
122     }
123
124     public void testAllowsSameInvocationMultipleTimes() {
125         mock.stubs().method("hello").id("hello #1");
126         mock.stubs().method("hello").after("hello #1").id("hello #2");
127         mock.stubs().method("hello").after("hello #2").id("hello #3");
128         mock.stubs().method("goodbye").after("hello #3");
129
130         proxy.hello();
131         proxy.hello();
132         proxy.hello();
133         proxy.goodbye();
134     }
135
136     public void testDetectsDuplicateIDs() {
137         String JavaDoc duplicateID = "DUPLICATE-ID";
138
139         mock.stubs().method("hello").id(duplicateID);
140
141         try {
142             mock.stubs().method("hello").id(duplicateID);
143         }
144         catch (AssertionFailedError ex) {
145             AssertMo.assertIncludes("error message contains duplicate id",
146                                     duplicateID, ex.getMessage());
147             return;
148         }
149         fail("should have failed");
150     }
151
152     public void testDetectsMissingIDs() {
153         String JavaDoc missingID = "MISSING-ID";
154
155         try {
156             mock.stubs().method("hello").after(missingID);
157         }
158         catch (AssertionFailedError ex) {
159             AssertMo.assertIncludes("error message contains missing id",
160                                     missingID, ex.getMessage());
161             return;
162         }
163
164         fail("should have failed");
165     }
166 }
167
Popular Tags