| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| MockObject |
|
| 0.0;0 |
| 1 | /* | |
| 2 | */ | |
| 3 | package biz.xsoftware.mock; | |
| 4 | ||
| 5 | import java.lang.reflect.Method; | |
| 6 | ||
| 7 | /** | |
| 8 | * The interface all mock objects implement. This is the interface used by the | |
| 9 | * unit tests once the mock object is created. | |
| 10 | * | |
| 11 | * @author Dean Hiller | |
| 12 | */ | |
| 13 | public interface MockObject { | |
| 14 | ||
| 15 | /** | |
| 16 | * Field used to expect that no methods have been called. | |
| 17 | */ | |
| 18 | public static String NONE = "No method should have been called"; | |
| 19 | ||
| 20 | /** | |
| 21 | * Field used to expect any method so expect returns as soon as any method | |
| 22 | * is called. | |
| 23 | */ | |
| 24 | public static String ANY = "'Any method'"; | |
| 25 | ||
| 26 | /** | |
| 27 | * Waits for one and only one method to be called. If any methods are called | |
| 28 | * before or after this one(after can sometimes be caught by the MockObject | |
| 29 | * when the threading is not synchronous), then this call will throw an | |
| 30 | * ExpectFailedException. | |
| 31 | * | |
| 32 | * @param method The expected method. | |
| 33 | * @return An array of params that were passed to the methods called | |
| 34 | */ | |
| 35 | public CalledMethod expect(String method); | |
| 36 | ||
| 37 | /** | |
| 38 | * Waits for the specific method, defined by the {@link MethodSignature} to | |
| 39 | * be called. If any other methods are called before or after this one then | |
| 40 | * this call will throw an ExpectFailedException | |
| 41 | * | |
| 42 | * @param methodSignature The exact method signature of the expected method | |
| 43 | * | |
| 44 | * @return The method and params that were passed to the called method | |
| 45 | */ | |
| 46 | // public CalledMethod expect(MethodSignature methodSignature); | |
| 47 | ||
| 48 | ||
| 49 | /** | |
| 50 | * Waits for all the methods to be called. If any of the methods are not | |
| 51 | * called or are called out of order, this method throws an exception which | |
| 52 | * will fail the test case. For each method, this will wait WAIT_TIME | |
| 53 | * milliseconds for each method to be called. If the method is not called | |
| 54 | * within this period, an exception will be thrown saying 'method' was not | |
| 55 | * called within timeout period. | |
| 56 | * | |
| 57 | * @param methods The expected method(s) in the correct order. | |
| 58 | * @return An array or arrays of params that were passed to the methods | |
| 59 | * called | |
| 60 | */ | |
| 61 | // public CalledMethod[] expect(MethodSignature firstMethod, MethodSignature... methodSignatures); | |
| 62 | ||
| 63 | /** | |
| 64 | * Waits for all the methods to be called. If any of the methods are not | |
| 65 | * called or are called out of order, this method throws an exception which | |
| 66 | * will fail the test case. For each method, this will wait WAIT_TIME | |
| 67 | * milliseconds for each method to be called. If the method is not called | |
| 68 | * within this period, an exception will be thrown saying 'method' was not | |
| 69 | * called within timeout period. | |
| 70 | * | |
| 71 | * @param methods The expected method(s) in the correct order. | |
| 72 | * @return An array or arrays of params that were passed to the methods | |
| 73 | * called | |
| 74 | */ | |
| 75 | public CalledMethod[] expect(String... methods); | |
| 76 | ||
| 77 | /** | |
| 78 | * Waits for a methods to be called a specific number of times. If any of | |
| 79 | * the methods, this method throws an exception which will fail the test | |
| 80 | * case. For each method, this will wait WAIT_TIME milliseconds for each | |
| 81 | * method to be called. If the method is not called within this period, an | |
| 82 | * exception will be thrown saying 'method' was not called within timeout | |
| 83 | * period. | |
| 84 | * | |
| 85 | * @param method | |
| 86 | * @param times is the number of times the methods is expected to be called | |
| 87 | * @return An array or arrays of params that were passed to the methods | |
| 88 | * called | |
| 89 | */ | |
| 90 | public CalledMethod[] expect(String method, int times); | |
| 91 | ||
| 92 | public CalledMethod expectUnordered(String method); | |
| 93 | ||
| 94 | /** | |
| 95 | * Expects the given methods to be called in no particular order | |
| 96 | * | |
| 97 | * @param methods The methods to expect | |
| 98 | * | |
| 99 | * @return The methods that were called with their parameters | |
| 100 | */ | |
| 101 | public CalledMethod[] expectUnordered(String... methods); | |
| 102 | ||
| 103 | /** | |
| 104 | * This method will expect the given method one time and ignore any further | |
| 105 | * calls to the method. This is useful for cases where the method maybe | |
| 106 | * called many times, but you just want to verify it happened at least once | |
| 107 | * | |
| 108 | * @param methods The method names to ignore | |
| 109 | * | |
| 110 | * @return The information about the method called | |
| 111 | */ | |
| 112 | public CalledMethod[] expectOnceThenIgnore(String... methods); | |
| 113 | ||
| 114 | /** | |
| 115 | * @deprecated Use {@link #expect(String)} now | |
| 116 | */ | |
| 117 | @Deprecated | |
| 118 | public CalledMethod expectCall(String method); | |
| 119 | ||
| 120 | /** | |
| 121 | * This method will call addIgnore for the given methods, run expect with | |
| 122 | * the given method and then remove all of the ignored methods | |
| 123 | * | |
| 124 | * @deprecated Use a combination of {@link #addIgnore(String[])} and | |
| 125 | * {@link #expect(String)} | |
| 126 | */ | |
| 127 | public CalledMethod expectCall(String method, String... ignoredMethods); | |
| 128 | ||
| 129 | /** | |
| 130 | * @deprecated Use {@link #expect(String[])} instead | |
| 131 | */ | |
| 132 | @Deprecated | |
| 133 | public CalledMethod[] expectOrderedCalls(String... methods); | |
| 134 | ||
| 135 | /** | |
| 136 | * @deprecated This is no longer supported, the use of | |
| 137 | * {@link #addIgnore(String[])} in combination with | |
| 138 | * {@link #expect(String[])} should be used | |
| 139 | */ | |
| 140 | @Deprecated | |
| 141 | public CalledMethod[] expectOrderedCalls(String[] methods, | |
| 142 | String[] ignoredMethods); | |
| 143 | ||
| 144 | /** | |
| 145 | * @deprecated No longer supported | |
| 146 | */ | |
| 147 | @Deprecated | |
| 148 | public CalledMethod[] expectUnorderedCalls(String... methods); | |
| 149 | ||
| 150 | /** | |
| 151 | * Calls {@link #addIgnore(String[])} with the given ignored methods, then | |
| 152 | * runs {@link #expectUnorderedCalls(String[])}, saving the result. Then | |
| 153 | * removes the ignored methods and finally returns the saved result | |
| 154 | * | |
| 155 | * @deprecated This is no longer supported, the use of | |
| 156 | * {@link #addIgnore(String[])} in combination with | |
| 157 | * {@link #expect(String[])} should be used | |
| 158 | */ | |
| 159 | @Deprecated | |
| 160 | public CalledMethod[] expectUnorderedCalls(String[] methods, | |
| 161 | String[] ignoredMethods); | |
| 162 | ||
| 163 | /** | |
| 164 | * Set the DefaultReturnValue for a 'method'. This value is returned if none | |
| 165 | * of the following happen | |
| 166 | * <ol> | |
| 167 | * <li>addReturnValue is not called giving a return value</li> | |
| 168 | * <li>addThrowException is not called returning an exception</li> | |
| 169 | * <li>addBehavior is not called running a snippet of code that returns a | |
| 170 | * value</li> | |
| 171 | * </ol> | |
| 172 | * | |
| 173 | * @param method The method name to set the default return value for | |
| 174 | * @param returnValue | |
| 175 | */ | |
| 176 | public void setDefaultReturnValue(String method, Object returnValue); | |
| 177 | ||
| 178 | /** | |
| 179 | * Provide the return value for a specific {@link Method}. This is useful | |
| 180 | * for specifying a method that you're having trouble getting MockObject to | |
| 181 | * handle correctly. | |
| 182 | * <p/> | |
| 183 | * For example: | |
| 184 | * <code> | |
| 185 | * MockObject m = MockObjectFactory.createMock(Runnable.class); | |
| 186 | * Method method = Object.class.getMethod("toString"); | |
| 187 | * m.setDefaultReturnValue(method, "test"); | |
| 188 | * assertEquals("test", m.toString()); | |
| 189 | * </code> | |
| 190 | * <p/> | |
| 191 | * | |
| 192 | * @param method The method to tie the return value to. | |
| 193 | * @param returnValue The value to return when the method is called | |
| 194 | */ | |
| 195 | public void setDefaultReturnValue(Method method, Object returnValue); | |
| 196 | ||
| 197 | /** | |
| 198 | * Set the DefaultBehavior for a 'method'. This Behavior object is used to | |
| 199 | * run a snippet of code if none of the following happen | |
| 200 | * <ol> | |
| 201 | * <li>addReturnValue is not called giving a return value</li> | |
| 202 | * <li>addThrowException is not called returning an exception</li> | |
| 203 | * <li>addBehavior is not called running a snippet of code that returns a | |
| 204 | * value</li> | |
| 205 | * </ol> | |
| 206 | * | |
| 207 | * See the Behavior class for more info on what to put in the Behavior | |
| 208 | * implementation. | |
| 209 | * | |
| 210 | * @param method When this method is called, the Behavior will be run. | |
| 211 | * @param b The snippet of code to be run. | |
| 212 | */ | |
| 213 | public void setDefaultBehavior(String method, Behavior b); | |
| 214 | ||
| 215 | /** | |
| 216 | * Set the DefaultBehavior for a 'method'. This Behavior object is used to | |
| 217 | * run a snippet of code if none of the following happen | |
| 218 | * <ol> | |
| 219 | * <li>addReturnValue is not called giving a return value</li> | |
| 220 | * <li>addThrowException is not called returning an exception</li> | |
| 221 | * <li>addBehavior is not called running a snippet of code that returns a | |
| 222 | * value</li> | |
| 223 | * </ol> | |
| 224 | * | |
| 225 | * A Behavior or CloningBehavior can be passed in. See the Behavior or | |
| 226 | * CloningBehavior class for more info on what to put in the Behavior | |
| 227 | * implementation. | |
| 228 | * | |
| 229 | * @param method When this method is called, the Behavior will be run. | |
| 230 | * @param b The snippet of code to be run. | |
| 231 | * @param argTypes The parameters types of the method so overloaded method | |
| 232 | * can be specified | |
| 233 | * | |
| 234 | * @deprecated Since 2007 Aug 8. Use | |
| 235 | * {@link #setDefaultBehavior(MethodSignature, Behavior)} | |
| 236 | * instead | |
| 237 | */ | |
| 238 | @Deprecated | |
| 239 | public void setDefaultBehavior(String method, Behavior b, | |
| 240 | Class<?>... argTypes); | |
| 241 | ||
| 242 | /** | |
| 243 | * Set the DefaultBehavior for a 'method'. This Behavior object is used to | |
| 244 | * run a snippet of code if none of the following happen | |
| 245 | * <ol> | |
| 246 | * <li>addReturnValue is not called giving a return value</li> | |
| 247 | * <li>addThrowException is not called returning an exception</li> | |
| 248 | * <li>addBehavior is not called running a snippet of code that returns a | |
| 249 | * value</li> | |
| 250 | * </ol> | |
| 251 | * | |
| 252 | * A Behavior or CloningBehavior can be passed in. See the Behavior or | |
| 253 | * CloningBehavior class for more info on what to put in the Behavior | |
| 254 | * implementation. | |
| 255 | * | |
| 256 | * @param methodSignature Used to define which method the behavior should be | |
| 257 | * assigned to | |
| 258 | * @param b The snippet of code to be run. | |
| 259 | */ | |
| 260 | public void setDefaultBehavior(MethodSignature methodSignature, Behavior b); | |
| 261 | ||
| 262 | /** | |
| 263 | * Add an exception to throw when a method on the mockObject is called. <br/<br/> | |
| 264 | * This can be called many times where each time it is called, the exception | |
| 265 | * is added to a queue. Each time 'method' is called, it checks the queue, | |
| 266 | * if empty, it does not throw an exception. <br/<br/> Should only pass the | |
| 267 | * type of Throwable that the 'method' can throw. If you pass IOException in | |
| 268 | * for a method that doesn't have a signature of 'throws IOException', then | |
| 269 | * one of the following exceptions will be thrown into the sysUnderTest | |
| 270 | * <p> | |
| 271 | * This method only expects the method name. It will search for a method | |
| 272 | * name that matches ignoring any method parameters. If more than one | |
| 273 | * methods are found then an exception will be thrown. In this case use | |
| 274 | * {@link #addThrowException(MethodSignature, Throwable)} instead | |
| 275 | * | |
| 276 | * <ol> | |
| 277 | * <li> UndeclaredThrowableException for MockObjects created with | |
| 278 | * MockCreator</li> | |
| 279 | * <li> RuntimeException for Subclasses of MockSuperclass</li> | |
| 280 | * </ol> | |
| 281 | * | |
| 282 | * @param method The method to throw the exception on when it is called. | |
| 283 | * @param e The exception to throw on method. | |
| 284 | */ | |
| 285 | public void addThrowException(String method, Throwable e); | |
| 286 | ||
| 287 | /** | |
| 288 | * Add an exception to throw when a method on the mockObject is called. <p/> | |
| 289 | * This can be called many times where each time it is called, the exception | |
| 290 | * is added to a queue. Each time 'method' is called, it checks the queue, | |
| 291 | * if empty, it does not throw an exception. <br/> Should only pass the type | |
| 292 | * of Throwable that the 'method' can throw. If you pass IOException in for | |
| 293 | * a method that doesn't have a signature of 'throws IOException', then one | |
| 294 | * of the following exceptions will be thrown into the sysUnderTest | |
| 295 | * <ol> | |
| 296 | * <li> UndeclaredThrowableException for MockObjects created with | |
| 297 | * MockCreator</li> | |
| 298 | * <li> RuntimeException for Subclasses of MockSuperclass</li> | |
| 299 | * </ol> | |
| 300 | * | |
| 301 | * @param method | |
| 302 | * @param e | |
| 303 | */ | |
| 304 | public void addThrowException(MethodSignature method, Throwable e); | |
| 305 | ||
| 306 | /** | |
| 307 | * Add a return value to return when 'method' is called. <br> | |
| 308 | * </br> This can be called multiple times and each call will add to a | |
| 309 | * queue. When 'method' is called, it will return the first value on the | |
| 310 | * queue. If the queue is null, 'method' will return the default value which | |
| 311 | * is null unless setDefaultReturnValue is called on MockObject. <br> | |
| 312 | * </br> Use Integer to return int, Long for long, etc. | |
| 313 | * | |
| 314 | * @param method The method that when called returns first value on queue | |
| 315 | * @param o The object or objects to return that is added to the queue | |
| 316 | */ | |
| 317 | public <T extends Object> void addReturnValue(String method, T... o); | |
| 318 | ||
| 319 | /** | |
| 320 | * Add a return value to return when 'method' is called. <br> | |
| 321 | * </br> This can be called multiple times and each call will add to a | |
| 322 | * queue. When 'method' is called, it will return the first value on the | |
| 323 | * queue. If the queue is null, 'method' will return the default value which | |
| 324 | * is null unless setDefaultReturnValue is called on MockObject. <br> | |
| 325 | * </br> Use Integer to return int, Long for long, etc. | |
| 326 | */ | |
| 327 | public <T extends Object> void addReturnValue(MethodSignature methodSignature, T... o); | |
| 328 | ||
| 329 | /** | |
| 330 | * Add a return value to return when 'method' is called. <br> | |
| 331 | * </br> This can be called multiple times and each call will add to a | |
| 332 | * queue. When 'method' is called, it will return the first value on the | |
| 333 | * queue. If the queue is null, 'method' will return the default value which | |
| 334 | * is null unless setDefaultReturnValue is called on MockObject. <br> | |
| 335 | * </br> Use Integer to return int, Long for long, etc. | |
| 336 | */ | |
| 337 | public <T extends Object> void addReturnValue(Method method, T... o); | |
| 338 | ||
| 339 | /** | |
| 340 | * @deprecated {@link #addReturnValue(Method, Object...)} was enhanced to detect if the given method returns | |
| 341 | * an array. If it does then the object array passed in will be the object returned for a single method call | |
| 342 | */ | |
| 343 | @Deprecated | |
| 344 | public void addReturnValue(String method,boolean isArray, Object ... o); | |
| 345 | ||
| 346 | /** | |
| 347 | * This method will look at the given behavior and will automatically call | |
| 348 | * {@link #setDefaultBehavior(String, Behavior, Class[])} for all the | |
| 349 | * methods that have the {@link BehaviorMethod} annotation | |
| 350 | * | |
| 351 | * @param behavior | |
| 352 | */ | |
| 353 | public void setBehavior(Behavior behavior); | |
| 354 | ||
| 355 | /** | |
| 356 | * Adds a snippet of code to be run which exists in the Behavior | |
| 357 | * implementation. A Behavior or CloningBehavior can be passed in. See the | |
| 358 | * Behavior or CloningBehavior class for more info on what to put in the | |
| 359 | * Behavior implementation. | |
| 360 | * | |
| 361 | * @param method When this method is called, the Behavior will be run | |
| 362 | * @param behavior The snippet of code to be run. | |
| 363 | */ | |
| 364 | public void addBehavior(String method, Behavior behavior); | |
| 365 | ||
| 366 | /** | |
| 367 | * Adds a snippet of code to be run which exists in the Behavior | |
| 368 | * implementation. A Behavior or CloningBehavior can be passed in. See the | |
| 369 | * Behavior or CloningBehavior class for more info on what to put in the | |
| 370 | * Behavior implementation. | |
| 371 | * | |
| 372 | * @param method When this method is called, the Behavior will be run | |
| 373 | * @param behavior The snippet of code to be run. | |
| 374 | * @param argTypes The parameters to the method to distinguish between | |
| 375 | * overloaded methods. | |
| 376 | * | |
| 377 | * @deprecated Since 2007 Aug 8. Use | |
| 378 | * {@link #addBehavior(MethodSignature, Behavior)} instead | |
| 379 | */ | |
| 380 | @Deprecated | |
| 381 | public void addBehavior(String method, Behavior behavior, | |
| 382 | Class<?>... argTypes); | |
| 383 | ||
| 384 | /** | |
| 385 | * Adds a snippet of code to be run which exists in the Behavior | |
| 386 | * implementation. A Behavior or CloningBehavior can be passed in. See the | |
| 387 | * Behavior or CloningBehavior class for more info on what to put in the | |
| 388 | * Behavior implementation. | |
| 389 | * | |
| 390 | * @param methodSignature Used to define which method the behavior should be | |
| 391 | * assigned to | |
| 392 | * | |
| 393 | * @param behavior The snippet of code to be run. | |
| 394 | */ | |
| 395 | public void addBehavior(MethodSignature methodSignature, Behavior behavior); | |
| 396 | ||
| 397 | /** | |
| 398 | * When calling expect, the MockObject will ignore the methods in 'methods' | |
| 399 | * variable so if one of the methods in this array is called, it will not | |
| 400 | * result in an exception | |
| 401 | * | |
| 402 | * @param method The name of the method(s) to ignore | |
| 403 | */ | |
| 404 | public void addIgnore(String... method); | |
| 405 | ||
| 406 | /** | |
| 407 | * When calling expect, the MockObject will ignore the methods in 'methods' | |
| 408 | * variable so if one of the methods in this array is called, it will not | |
| 409 | * result in an exception. | |
| 410 | * | |
| 411 | * @param methodSignature The {@link MethodSignature} to add to the list of | |
| 412 | * ignored methods | |
| 413 | */ | |
| 414 | public void addIgnore(MethodSignature... methodSignature); | |
| 415 | ||
| 416 | /** | |
| 417 | * Removes the method(s) from the ignored methods set | |
| 418 | * | |
| 419 | * @param methods The name of the method(s) to remove from the list of | |
| 420 | * ignored methods | |
| 421 | */ | |
| 422 | public void removeIgnore(String... methods); | |
| 423 | ||
| 424 | /** | |
| 425 | * Removes the method from the ignored methods set. | |
| 426 | * | |
| 427 | * @param methodSignature The {@link MethodSignature} to remove from the | |
| 428 | * ignore list | |
| 429 | */ | |
| 430 | public void removeIgnore(MethodSignature... methodSignature); | |
| 431 | ||
| 432 | /** | |
| 433 | * Sets the timeout which is the delay that a mockobject waits for a method | |
| 434 | * to be called before failing. The default is 5 seconds. | |
| 435 | */ | |
| 436 | public void setExpectTimeout(int timeout); | |
| 437 | ||
| 438 | /** | |
| 439 | * Gets the timeout which is the delay that a mockobject waits for a method | |
| 440 | * to be called before failing. | |
| 441 | * | |
| 442 | * @return The current expect timeout duration | |
| 443 | */ | |
| 444 | public int getExpectTimeout(); | |
| 445 | ||
| 446 | /** | |
| 447 | * This is used if you want to ignore case sensitivity. You could then have | |
| 448 | * a method interface.checkWorks() and successfully run | |
| 449 | * mockObj.expect("checkworks") | |
| 450 | * | |
| 451 | * @param isCaseSensitive | |
| 452 | */ | |
| 453 | public void setCaseSensitive(boolean isCaseSensitive); | |
| 454 | ||
| 455 | /** | |
| 456 | * Returns the current setting for case sensitivity | |
| 457 | */ | |
| 458 | public boolean isCaseSensitive(); | |
| 459 | ||
| 460 | /** | |
| 461 | * This method will reset the MockObject to an empty state. This means it | |
| 462 | * will clear all of the called methods and remove all the ignores that were | |
| 463 | * added. In addition it will remove all of the set return values and | |
| 464 | * behaviors | |
| 465 | */ | |
| 466 | public void reset(); | |
| 467 | } |