Clover Coverage Report - EasyMock 3.0
Coverage timestamp: sam. mai 8 2010 14:37:27 CEST
0   314   0   -
0   23   -   0
0     -  
1    
 
  IMockBuilder       Line # 123 0 - 0 0 - -1.0
 
No Tests
 
1    /**
2    * Copyright 2001-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10    * Unless required by applicable law or agreed to in writing, software
11    * distributed under the License is distributed on an "AS IS" BASIS,
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    * See the License for the specific language governing permissions and
14    * limitations under the License.
15    */
16   
17    package org.easymock;
18   
19    import java.lang.reflect.Constructor;
20    import java.lang.reflect.Method;
21   
22    /**
23    * Helps the creation of partial mocks with {@link EasyMock}.
24    *
25    * <p>
26    * Example of usage:
27    *
28    * <pre>
29    * public class MyClass {
30    * public MyClass(A a, B b) {
31    * }
32    * }
33    *
34    * public class MyClassTest {
35    * &#064;Test
36    * public void testFoo() throws Exception {
37    * IMocksControl mockControl = createControl();
38    * A a = mockControl.createMock(A.class);
39    * B b = mockControl.createMock(B.class);
40    *
41    * MyClass myClass = createMockBuilder(MyClass.class)
42    * .withConstructor(a, b).createMock(mockControl);
43    *
44    * // Set the expectations of A and B and test some method in MyClass
45    * }
46    * }
47    * </pre>
48    *
49    * <p>
50    * This class also has support for partial mocks as shown by the example below:
51    *
52    * <pre>
53    * public class MyMockedClass {
54    * // Empty class is also valid for {@link IMockBuilder}.
55    * public MyMockedClass() {
56    * }
57    *
58    * public void foo(int a) {
59    * blah(a);
60    * bleh();
61    * }
62    *
63    * public void blah(int a) {
64    * }
65    *
66    * public void bleh() {
67    * }
68    * }
69    *
70    * public class MyMockedClassTest {
71    * &#064;Test
72    * public void testFoo() throws Exception {
73    * MyMockedClass myMockedClass = createMockBuilder(MyMockedClass.class)
74    * .withConstructor().addMockedMethod(&quot;blah&quot;, int.class)
75    * .addMockedMethod(&quot;bleh&quot;).createMock();
76    *
77    * // These are the expectations.
78    * myMockedClass.blah(1);
79    * myMockedClass.bleh();
80    * replay(myMockedClass);
81    *
82    * myMockedClass.foo(1);
83    * verify(myMockedClass);
84    * }
85    * }
86    * </pre>
87    *
88    * <p>
89    * Warning: There may be ambiguities when there are two different constructors
90    * with compatible types. For instance:
91    *
92    * <pre>
93    * public class A {
94    * }
95    *
96    * public class B extends A {
97    * }
98    *
99    * public class ClassWithAmbiguity {
100    * public ClassWithAmbiguity(A a) {
101    * }
102    *
103    * public ClassWithAmbiguity(B b) {
104    * }
105    * }
106    * </pre>
107    *
108    * will cause problems if using {@link #withConstructor(Object...)}. To solve
109    * this, you can explicitly define the constructor parameter types to use by
110    * calling {@link #withConstructor(Class...)} and then
111    * {@link #withArgs(Object...)}, like this:
112    *
113    * <pre>
114    * createMockBuilder(MyMockedClass.class).withConstructor(A.class).withArgs(
115    * new A()).createMock();
116    * </pre>
117    *
118    * @param <T>
119    * type of the object being created
120    *
121    * @author Henri Tremblay
122    */
 
123    public interface IMockBuilder<T> {
124   
125    /**
126    * Adds a method to be mocked in the testing class. Each call will add a new
127    * method to the result mock.
128    *
129    * The method is searched for in the class itself as well as superclasses.
130    *
131    * @param method
132    * method to be mocked
133    * @return this
134    */
135    IMockBuilder<T> addMockedMethod(Method method);
136   
137    /**
138    * Adds a method to be mocked in the testing class. Each call will add a new
139    * method to the result mock.
140    *
141    * The method is searched for in the class itself as well as superclasses.
142    *
143    * There must be no overload of the method. You will have to rely on the
144    * other <code>addMockedMethod</code>s in this class if that is the case.
145    *
146    * @param methodName
147    * name of the method to be mocked
148    * @return this
149    */
150    IMockBuilder<T> addMockedMethod(String methodName);
151   
152    /**
153    * Adds a method to be mocked in the testing class. Each call will add a new
154    * method to the result mock.
155    *
156    * The method is searched for in the class itself as well as superclasses.
157    *
158    * @param methodName
159    * name of the method to be mocked
160    * @param parameterTypes
161    * types of the parameters of the method
162    * @return this
163    */
164    IMockBuilder<T> addMockedMethod(String methodName, Class<?>... parameterTypes);
165   
166    /**
167    * Adds methods to be mocked in the testing class. Same as
168    * {@link #addMockedMethod(String)} but to mock many methods at once.
169    *
170    * @param methodNames
171    * names of the methods to be mocked
172    * @return this
173    */
174    IMockBuilder<T> addMockedMethods(String... methodNames);
175   
176    /**
177    * Adds methods to be mocked in the testing class. Same as
178    * {@link #addMockedMethod(Method)} but to mock many methods at once.
179    *
180    * @param methods
181    * methods to be mocked
182    * @return this
183    */
184    IMockBuilder<T> addMockedMethods(Method... methods);
185   
186    /**
187    * Defines the constructor to use to instantiate the mock. It is expected
188    * that you call {@link #withArgs} with the actual constructor argument
189    * values after this.
190    *
191    * @param constructor
192    * the constructor to be called
193    * @return this
194    */
195    IMockBuilder<T> withConstructor(Constructor<?> constructor);
196   
197    /**
198    * Defines the empty constructor should be called.
199    *
200    * @return this
201    */
202    IMockBuilder<T> withConstructor();
203   
204    /**
205    * Defines the constructor parameters for the mocked class. The builder will
206    * automatically find a constructor with compatible argument types. This
207    * throws an exception if there is more than one constructor which would
208    * accept the given parameters.
209    *
210    * @param initArgs
211    * arguments of the constructor
212    * @return this
213    */
214    IMockBuilder<T> withConstructor(Object... initArgs);
215   
216    /**
217    * Defines the exact argument types for the constructor to use. It is
218    * expected that you call {@link #withArgs} with the actual constructor
219    * argument values after this.
220    *
221    * @param argTypes
222    * the exact argument types of the constructor
223    * @return this
224    */
225    IMockBuilder<T> withConstructor(Class<?>... argTypes);
226   
227    /**
228    * Defines the arguments to be passed to the constructor of the class. The
229    * types of the arguments must match those previously defined with
230    * {@link #withConstructor(Class...)} or
231    * {@link #withConstructor(Constructor)}.
232    *
233    * @param initArgs
234    * the arguments to pass to the constructor
235    * @return this
236    */
237    IMockBuilder<T> withArgs(Object... initArgs);
238   
239    /**
240    * Create a strict mock from this builder. The same builder can be called to
241    * create multiple mocks.
242    *
243    * @return the newly created mock
244    */
245    T createStrictMock();
246   
247    /**
248    * Create a default mock from this builder. The same builder can be called
249    * to create multiple mocks.
250    *
251    * @return the newly created mock
252    */
253    T createMock();
254   
255    /**
256    * Create a nice mock from this builder. The same builder can be called to
257    * create multiple mocks.
258    *
259    * @return the newly created mock
260    */
261    T createNiceMock();
262   
263    /**
264    * Create mock from the provided mock control using the arguments passed to
265    * the builder.
266    *
267    * @param control
268    * {@link org.easymock.IMocksControl} used to create the object
269    * @return the newly created mock
270    */
271    T createMock(IMocksControl control);
272   
273    /**
274    * Create a named strict mock from this builder. The same builder can be
275    * called to create multiple mocks.
276    *
277    * @param name
278    * the mock name
279    * @return the newly created mock
280    */
281    T createStrictMock(String name);
282   
283    /**
284    * Create named mock from the provided mock control using the arguments
285    * passed to the builder.
286    *
287    * @param name
288    * the mock name
289    * @return the newly created mock
290    */
291    T createMock(String name);
292   
293    /**
294    * Create a named nice mock from this builder. The same builder can be
295    * called to create multiple mocks.
296    *
297    * @param name
298    * the mock name
299    * @return the newly created mock
300    */
301    T createNiceMock(String name);
302   
303    /**
304    * Create named mock from the provided mock control using the arguments
305    * passed to the builder.
306    *
307    * @param name
308    * the mock name
309    * @param control
310    * {@link org.easymock.IMocksControl} used to create the object
311    * @return the newly created mock
312    */
313    T createMock(String name, IMocksControl control);
314    }