1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package net.sf.surrogate.core;
30
31 import junit.framework.AssertionFailedError;
32 import junit.framework.TestCase;
33
34 /***
35 * Tests the MockMethod internals.
36 * <p>
37 *
38 * @author Per S Hustad
39 */
40 public class MockMethodTestCase extends TestCase {
41
42 public MockMethodTestCase(String name) {
43 super(name);
44 }
45
46 /***
47 * Test MockMethod standard behavior
48 */
49 public void testStandardExecute() throws Throwable {
50
51 MockMethod mockMethod = new MockMethod(TestClass01.class, "testCall",
52 new Class[] { String.class, Integer.TYPE });
53
54 mockMethod.addReturnValue("1");
55 mockMethod.addReturnValue("2");
56 mockMethod.addExpectedParameters(new Object[] { "A", new Integer(1) });
57 mockMethod.addExpectedParameters(new Object[] { "B", new Integer(2) });
58 mockMethod.addExpectedParameters(new Object[] { "C", new Integer(3) });
59
60 mockMethod.setExpectedCalls(2);
61 Object testRet = mockMethod.execute(new Object[] { "A", new Integer(1) });
62 assertEquals("1", testRet);
63
64
65 try {
66 mockMethod.verify();
67 fail("Verify should fail - not exact calls");
68 } catch (AssertionFailedError e) {
69 }
70 testRet = mockMethod.execute(new Object[] { "B", new Integer(2) });
71 assertEquals("2", testRet);
72 mockMethod.verify();
73
74 try {
75 testRet = mockMethod.execute(new Object[] { "C", new Integer(3) });
76 fail("Expected exception because called more than specified");
77 } catch (AssertionFailedError e) {
78 }
79
80 mockMethod.setExpectedCalls(3);
81 try {
82 testRet = mockMethod.execute(new Object[] { "WRONG", new Integer(3) });
83 fail("Expected AssertionFailedError - wrong arguments");
84 } catch (AssertionFailedError e) {
85 }
86
87 try {
88 mockMethod.addExpectedParameters(new Object[] { "B" });
89 fail("Expected an IllegalArgumentException - to few arguments");
90 } catch (IllegalArgumentException e) {
91
92 }
93
94 }
95
96 /***
97 * Test MockMethod for method with no arguments and void return
98 *
99 * @throws Throwable
100 * if TestCase error
101 */
102 public void testExecuteWithVoid() throws Throwable {
103 MockMethod mockMethod = new MockMethod(TestClass01.class, "noArgsAndVoid");
104 mockMethod.setExpectedCalls(1);
105 mockMethod.execute(null);
106 mockMethod.verify();
107 mockMethod.reset();
108
109 try {
110 mockMethod.addReturnValue(new Object());
111 fail("Expected an IllegalArgumentException - no return for void method");
112 } catch (IllegalArgumentException e) {
113
114 }
115
116 }
117
118 /***
119 * Test wrong arguments to constructors
120 *
121 * @throws Throwable
122 */
123 public void testConstructors() throws Throwable {
124
125 new MockMethod(TestClass01.class, new Class[] { String.class, Integer.TYPE });
126 new MockMethod(TestClass01.class, "testCall", new Class[] { String.class, Integer.TYPE });
127 new MockMethod(OverloadedTestClass.class, new Class[] { String.class, Integer.TYPE });
128 new MockMethod(OverloadedTestClass.class, new Class[] { String.class, Integer.TYPE, String.class });
129 new MockMethod(OverloadedTestClass.class, "testCall", new Class[] { String.class, Integer.TYPE });
130 new MockMethod(OverloadedTestClass.class, "testCall", new Class[] { String.class, Integer.TYPE, String.class });
131
132
133 try {
134 new MockMethod(TestClass01.class, "testCall", new Class[] { String.class, Integer.TYPE, String.class });
135 fail("Expected NoSuchMethodException");
136 } catch (NoSuchMethodException e) {
137 }
138
139
140 try {
141 new MockMethod(TestClass01.class, "testCallNOT_EXISTING", new Class[] { String.class, Integer.TYPE });
142 fail("Expected NoSuchMethodException");
143 } catch (NoSuchMethodException e) {
144 }
145
146 try {
147 new MockMethod(OverloadedTestClass.class, "testCall");
148 fail("Expected IllegalArgumentException");
149 } catch (IllegalArgumentException e) {
150 }
151
152 }
153
154 /***
155 * Test the setMock method for Exceptions
156 *
157 * @see MockMethod#addThrowableReturnValue(Throwable)
158 */
159 public void testAddThrowableReturnValue() throws Throwable {
160
161 MockMethod mockMethod = new MockMethod(TestClass01.class, "noArgs");
162 mockMethod.addReturnValue("1");
163 mockMethod.addThrowableReturnValue(new TestException());
164 mockMethod.addReturnValue("2");
165 Object retVal = mockMethod.execute(null);
166 assertEquals("1", retVal);
167 try {
168 mockMethod.execute(null);
169 fail("expected a TestException");
170 } catch (TestException e) {
171 }
172 retVal = mockMethod.execute(null);
173 assertEquals("2", retVal);
174 }
175
176 private static class TestClass01 {
177
178 private TestClass01(String a, int b) {
179
180 }
181
182 private String testCall(String a, int b) {
183 return "OK";
184 }
185
186 private String noArgs() {
187 return "OK";
188 }
189
190 private void noArgsAndVoid() {
191
192 }
193 }
194
195 private static class OverloadedTestClass {
196 private OverloadedTestClass(String a, int b) {
197
198 }
199
200 private OverloadedTestClass(String a, int b, String c) {
201
202 }
203
204 private String testCall(String a, int b) {
205 return "OK";
206 }
207
208 private String testCall(String a, int b, String c) {
209 return "OK";
210 }
211 }
212
213 private static class TestException extends Exception {
214
215 }
216
217 }