1 /* 2 * Copyright (c) 2006 JMockit developers 3 * This file is subject to the terms of the MIT license (see LICENSE.txt). 4 */ 5 package mockit; 6 7 import edu.umd.cs.findbugs.annotations.NonNull; 8 import edu.umd.cs.findbugs.annotations.Nullable; 9 10 import java.lang.reflect.ParameterizedType; 11 import java.lang.reflect.Type; 12 import java.util.List; 13 import java.util.Map; 14 import java.util.regex.Pattern; 15 16 import mockit.internal.expectations.TestOnlyPhase; 17 import mockit.internal.expectations.argumentMatching.AlwaysTrueMatcher; 18 import mockit.internal.expectations.argumentMatching.ArgumentMatcher; 19 import mockit.internal.expectations.argumentMatching.CaptureMatcher; 20 import mockit.internal.expectations.argumentMatching.ClassMatcher; 21 import mockit.internal.expectations.argumentMatching.HamcrestAdapter; 22 import mockit.internal.expectations.argumentMatching.InequalityMatcher; 23 import mockit.internal.expectations.argumentMatching.LenientEqualityMatcher; 24 import mockit.internal.expectations.argumentMatching.NonNullityMatcher; 25 import mockit.internal.expectations.argumentMatching.NullityMatcher; 26 import mockit.internal.expectations.argumentMatching.NumericEqualityMatcher; 27 import mockit.internal.expectations.argumentMatching.PatternMatcher; 28 import mockit.internal.expectations.argumentMatching.ReflectiveMatcher; 29 import mockit.internal.expectations.argumentMatching.SamenessMatcher; 30 import mockit.internal.expectations.argumentMatching.StringContainmentMatcher; 31 import mockit.internal.expectations.argumentMatching.StringPrefixMatcher; 32 import mockit.internal.expectations.argumentMatching.StringSuffixMatcher; 33 import mockit.internal.startup.Startup; 34 import mockit.internal.util.DefaultValues; 35 36 import org.checkerframework.checker.index.qual.NonNegative; 37 import org.hamcrest.Matcher; 38 39 /** 40 * Provides common API for use inside {@linkplain Expectations expectation} and {@linkplain Verifications verification} 41 * blocks. 42 */ 43 @SuppressWarnings("ClassWithTooManyFields") 44 class Invocations { 45 static { 46 Startup.verifyInitialization(); 47 } 48 49 /** 50 * Matches any <code>Object</code> reference received by a parameter of a reference type. 51 * <p> 52 * This field can only be used as the argument value at the proper parameter position in a method/constructor 53 * invocation, when recording or verifying an expectation; it cannot be used anywhere else. 54 * <p> 55 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 56 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 57 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 58 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 59 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 60 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 61 * <p> 62 * Notice the use of this field will usually require a cast to the specific parameter type. However, if there is any 63 * other parameter for which an argument matching constraint is specified, passing the <code>null</code> reference 64 * instead will have the same effect. 65 * <p> 66 * To match an entire <em>varargs</em> parameter of element type <code>V</code> (ie, all arguments in the array), 67 * cast it to the parameter type: "<code>(V[]) any</code>". 68 * 69 * @see #anyBoolean 70 * @see #anyByte 71 * @see #anyChar 72 * @see #anyDouble 73 * @see #anyFloat 74 * @see #anyInt 75 * @see #anyLong 76 * @see #anyShort 77 * @see #anyString 78 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#matcherFields" target="tutorial">Tutorial</a> 79 */ 80 @Nullable 81 protected final Object any = null; 82 83 /** 84 * Matches any <code>String</code> value received by a parameter of this type. 85 * <p> 86 * This field can only be used as the argument value at the proper parameter position in a method/constructor 87 * invocation, when recording or verifying an expectation; it cannot be used anywhere else. 88 * <p> 89 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 90 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 91 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 92 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 93 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 94 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 95 * 96 * @see #anyBoolean 97 * @see #anyByte 98 * @see #anyChar 99 * @see #anyDouble 100 * @see #anyFloat 101 * @see #anyInt 102 * @see #anyLong 103 * @see #anyShort 104 * @see #any 105 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#matcherFields" target="tutorial">Tutorial</a> 106 */ 107 // This is intentional: the empty string causes the compiler to not generate a field read, while the null reference 108 // is inconvenient with the invoke(...) methods: 109 @NonNull 110 protected final String anyString = new String(); 111 112 /** 113 * Matches any <code>long</code> or <code>Long</code> value received by a parameter of that type. 114 * <p> 115 * This field can only be used as the argument value at the proper parameter position in a method/constructor 116 * invocation, when recording or verifying an expectation; it cannot be used anywhere else. 117 * <p> 118 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 119 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 120 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 121 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 122 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 123 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 124 * 125 * @see #anyBoolean 126 * @see #anyByte 127 * @see #anyChar 128 * @see #anyDouble 129 * @see #anyFloat 130 * @see #anyInt 131 * @see #anyShort 132 * @see #anyString 133 * @see #any 134 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#matcherFields" target="tutorial">Tutorial</a> 135 */ 136 @NonNull 137 protected final Long anyLong = 0L; 138 139 /** 140 * Matches any <code>int</code> or <code>Integer</code> value received by a parameter of that type. 141 * <p> 142 * This field can only be used as the argument value at the proper parameter position in a method/constructor 143 * invocation, when recording or verifying an expectation; it cannot be used anywhere else. 144 * <p> 145 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 146 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 147 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 148 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 149 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 150 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 151 * 152 * @see #anyBoolean 153 * @see #anyByte 154 * @see #anyChar 155 * @see #anyDouble 156 * @see #anyFloat 157 * @see #anyLong 158 * @see #anyShort 159 * @see #anyString 160 * @see #any 161 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#matcherFields" target="tutorial">Tutorial</a> 162 */ 163 @NonNull 164 protected final Integer anyInt = 0; 165 166 /** 167 * Matches any <code>short</code> or <code>Short</code> value received by a parameter of that type. 168 * <p> 169 * This field can only be used as the argument value at the proper parameter position in a method/constructor 170 * invocation, when recording or verifying an expectation; it cannot be used anywhere else. 171 * <p> 172 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 173 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 174 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 175 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 176 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 177 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 178 * 179 * @see #anyBoolean 180 * @see #anyByte 181 * @see #anyChar 182 * @see #anyDouble 183 * @see #anyFloat 184 * @see #anyInt 185 * @see #anyLong 186 * @see #anyString 187 * @see #any 188 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#matcherFields" target="tutorial">Tutorial</a> 189 */ 190 @NonNull 191 protected final Short anyShort = 0; 192 193 /** 194 * Matches any <code>byte</code> or <code>Byte</code> value received by a parameter of that type. 195 * <p> 196 * This field can only be used as the argument value at the proper parameter position in a method/constructor 197 * invocation, when recording or verifying an expectation; it cannot be used anywhere else. 198 * <p> 199 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 200 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 201 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 202 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 203 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 204 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 205 * 206 * @see #anyBoolean 207 * @see #anyChar 208 * @see #anyDouble 209 * @see #anyFloat 210 * @see #anyInt 211 * @see #anyLong 212 * @see #anyShort 213 * @see #anyString 214 * @see #any 215 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#matcherFields" target="tutorial">Tutorial</a> 216 */ 217 @NonNull 218 protected final Byte anyByte = 0; 219 220 /** 221 * Matches any <code>boolean</code> or <code>Boolean</code> value received by a parameter of that type. 222 * <p> 223 * This field can only be used as the argument value at the proper parameter position in a method/constructor 224 * invocation, when recording or verifying an expectation; it cannot be used anywhere else. 225 * <p> 226 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 227 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 228 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 229 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 230 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 231 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 232 * 233 * @see #anyByte 234 * @see #anyChar 235 * @see #anyDouble 236 * @see #anyFloat 237 * @see #anyInt 238 * @see #anyLong 239 * @see #anyShort 240 * @see #anyString 241 * @see #any 242 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#matcherFields" target="tutorial">Tutorial</a> 243 */ 244 @NonNull 245 protected final Boolean anyBoolean = false; 246 247 /** 248 * Matches any <code>char</code> or <code>Character</code> value received by a parameter of that type. 249 * <p> 250 * This field can only be used as the argument value at the proper parameter position in a method/constructor 251 * invocation, when recording or verifying an expectation; it cannot be used anywhere else. 252 * <p> 253 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 254 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 255 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 256 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 257 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 258 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 259 * 260 * @see #anyBoolean 261 * @see #anyByte 262 * @see #anyDouble 263 * @see #anyFloat 264 * @see #anyInt 265 * @see #anyLong 266 * @see #anyShort 267 * @see #anyString 268 * @see #any 269 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#matcherFields" target="tutorial">Tutorial</a> 270 */ 271 @NonNull 272 protected final Character anyChar = '\0'; 273 274 /** 275 * Matches any <code>double</code> or <code>Double</code> value received by a parameter of that type. 276 * <p> 277 * This field can only be used as the argument value at the proper parameter position in a method/constructor 278 * invocation, when recording or verifying an expectation; it cannot be used anywhere else. 279 * <p> 280 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 281 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 282 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 283 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 284 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 285 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 286 * 287 * @see #anyBoolean 288 * @see #anyByte 289 * @see #anyChar 290 * @see #anyFloat 291 * @see #anyInt 292 * @see #anyLong 293 * @see #anyShort 294 * @see #anyString 295 * @see #any 296 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#matcherFields" target="tutorial">Tutorial</a> 297 */ 298 @NonNull 299 protected final Double anyDouble = 0.0; 300 301 /** 302 * Matches any <code>float</code> or <code>Float</code> value received by a parameter of that type. 303 * <p> 304 * This field can only be used as the argument value at the proper parameter position in a method/constructor 305 * invocation, when recording or verifying an expectation; it cannot be used anywhere else. 306 * <p> 307 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 308 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 309 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 310 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 311 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 312 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 313 * 314 * @see #anyBoolean 315 * @see #anyByte 316 * @see #anyChar 317 * @see #anyDouble 318 * @see #anyInt 319 * @see #anyLong 320 * @see #anyString 321 * @see #anyShort 322 * @see #any 323 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#matcherFields" target="tutorial">Tutorial</a> 324 */ 325 @NonNull 326 protected final Float anyFloat = 0.0F; 327 328 /** 329 * A non-negative value assigned to this field will be taken as the exact number of times that invocations matching 330 * the current expectation should occur during replay. 331 * 332 * @see #minTimes 333 * @see #maxTimes 334 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#constraints" target="tutorial">Tutorial</a> 335 */ 336 @NonNegative 337 protected int times; 338 339 /** 340 * A positive value assigned to this field will be taken as the minimum number of times that invocations matching 341 * the current expectation should occur during replay. <em>Zero</em> or a <em>negative</em> value means there is no 342 * lower limit. 343 * <p> 344 * If not specified, the default value of <code>1</code> (one) is used. 345 * <p> 346 * The <em>maximum</em> number of times is automatically adjusted to allow any number of invocations. Both 347 * <code>minTimes</code> and <code>maxTimes</code> can be specified for the same expectation, provided 348 * <code>minTimes</code> is assigned first. 349 * 350 * @see #times 351 * @see #maxTimes 352 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#constraints" target="tutorial">Tutorial</a> 353 */ 354 protected int minTimes; 355 356 /** 357 * A non-negative value assigned to this field will be taken as the maximum number of times that invocations 358 * matching the current expectation should occur during replay. A <em>negative</em> value implies there is no upper 359 * limit (the default). 360 * <p> 361 * Both <code>minTimes</code> and <code>maxTimes</code> can be specified for the same expectation, provided 362 * <code>minTimes</code> is assigned first. 363 * 364 * @see #times 365 * @see #minTimes 366 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#constraints" target="tutorial">Tutorial</a> 367 */ 368 protected int maxTimes; 369 370 /** The current phase. */ 371 @SuppressWarnings("NullableProblems") 372 @NonNull 373 TestOnlyPhase currentPhase; 374 375 /** 376 * Applies a <em>Hamcrest</em> argument matcher for a parameter in the current expectation. 377 * <p> 378 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 379 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 380 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 381 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 382 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 383 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 384 * 385 * @param <T> 386 * the generic type 387 * @param argumentMatcher 388 * any <em>org.hamcrest.Matcher</em> object 389 * 390 * @return the value recorded inside the given Hamcrest matcher, or <em>null</em> if there is no such value to be 391 * found 392 * 393 * @see #with(Delegate) Use {@linkplain #withCapture(List) argument capturing} or {@link #with(Delegate)} instead. 394 */ 395 @Nullable 396 protected final <T> T withArgThat(@NonNull Matcher<? super T> argumentMatcher) { 397 HamcrestAdapter matcher = new HamcrestAdapter(argumentMatcher); 398 addMatcher(matcher); 399 400 @SuppressWarnings("unchecked") 401 T argValue = (T) matcher.getInnerValue(); 402 return argValue; 403 } 404 405 /** 406 * Applies a custom argument matcher for a parameter in the current expectation. 407 * <p> 408 * The class of the given delegate object should define a single non-<code>private</code> <em>delegate</em> method 409 * (plus any number of helper <code>private</code> methods). The name of the delegate method doesn't matter, but it 410 * must have a single parameter capable of receiving the relevant argument values. 411 * <p> 412 * The return type of the delegate method should be <code>boolean</code> or <code>void</code>. In the first case, a 413 * return value of <code>true</code> will indicate a successful match for the actual invocation argument at replay 414 * time, while a return of <code>false</code> will fail to match the invocation. In the case of a <code>void</code> 415 * return type, the actual invocation argument should be validated through a suitable JUnit/TestNG assertion. 416 * <p> 417 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 418 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 419 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 420 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 421 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 422 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 423 * 424 * @param <T> 425 * the generic type 426 * @param objectWithDelegateMethod 427 * an instance of a class defining a single non-<code>private</code> delegate method 428 * 429 * @return the default primitive value corresponding to <code>T</code> if it's a primitive wrapper type, or 430 * <code>null</code> otherwise 431 * 432 * @see #withArgThat(org.hamcrest.Matcher) 433 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#withMethods" target="tutorial">Tutorial</a> 434 */ 435 @Nullable 436 protected final <T> T with(@NonNull Delegate<? super T> objectWithDelegateMethod) { 437 addMatcher(new ReflectiveMatcher(objectWithDelegateMethod)); 438 439 Class<?> delegateClass = objectWithDelegateMethod.getClass(); 440 Type[] genericInterfaces = delegateClass.getGenericInterfaces(); 441 442 while (genericInterfaces.length == 0) { 443 delegateClass = delegateClass.getSuperclass(); 444 genericInterfaces = delegateClass.getGenericInterfaces(); 445 } 446 447 Type typeFromDelegateImplementationClass = genericInterfaces[0]; 448 449 if (!(typeFromDelegateImplementationClass instanceof ParameterizedType)) { 450 return null; 451 } 452 453 Type parameterType = ((ParameterizedType) typeFromDelegateImplementationClass).getActualTypeArguments()[0]; 454 return DefaultValues.computeForWrapperType(parameterType); 455 } 456 457 /** 458 * Adds the matcher. 459 * 460 * @param matcher 461 * the matcher 462 */ 463 private void addMatcher(@NonNull ArgumentMatcher<?> matcher) { 464 currentPhase.addArgMatcher(matcher); 465 } 466 467 /** 468 * Same as {@link #withEqual(Object)}, but matching any argument value of the appropriate type (<code>null</code> 469 * included). 470 * <p> 471 * Consider using instead the "anyXyz" field appropriate to the parameter type: {@link #anyBoolean}, 472 * {@link #anyByte}, {@link #anyChar}, {@link #anyDouble}, {@link #anyFloat}, {@link #anyInt}, {@link #anyLong}, 473 * {@link #anyShort}, {@link #anyString}, or {@link #any} for other reference types. 474 * <p> 475 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 476 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 477 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 478 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 479 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 480 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 481 * 482 * @param <T> 483 * the generic type 484 * @param arg 485 * an arbitrary value which will match any argument value in the replay phase 486 * 487 * @return the input argument 488 * 489 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#withMethods" target="tutorial">Tutorial</a> 490 */ 491 @NonNull 492 protected final <T> T withAny(@NonNull T arg) { 493 ArgumentMatcher<?> matcher; 494 if (arg instanceof String) { 495 matcher = AlwaysTrueMatcher.ANY_STRING; 496 } else if (arg instanceof Integer) { 497 matcher = AlwaysTrueMatcher.ANY_INT; 498 } else if (arg instanceof Boolean) { 499 matcher = AlwaysTrueMatcher.ANY_BOOLEAN; 500 } else if (arg instanceof Character) { 501 matcher = AlwaysTrueMatcher.ANY_CHAR; 502 } else if (arg instanceof Double) { 503 matcher = AlwaysTrueMatcher.ANY_DOUBLE; 504 } else if (arg instanceof Float) { 505 matcher = AlwaysTrueMatcher.ANY_FLOAT; 506 } else if (arg instanceof Long) { 507 matcher = AlwaysTrueMatcher.ANY_LONG; 508 } else if (arg instanceof Byte) { 509 matcher = AlwaysTrueMatcher.ANY_BYTE; 510 } else if (arg instanceof Short) { 511 matcher = AlwaysTrueMatcher.ANY_SHORT; 512 } else { 513 matcher = AlwaysTrueMatcher.ANY_VALUE; 514 } 515 516 addMatcher(matcher); 517 return arg; 518 } 519 520 /** 521 * Captures the argument value passed into the associated expectation parameter, for each invocation that matches 522 * the expectation when the tested code is exercised. As each such value is captured, it gets added to the given 523 * list so that it can be inspected later. Apart from capturing received argument values, this method has the same 524 * effect as the {@link #any} argument matcher. 525 * <p> 526 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 527 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 528 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 529 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 530 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 531 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 532 * 533 * @param <T> 534 * the generic type 535 * @param valueHolderForMultipleInvocations 536 * list into which the arguments received by matching invocations will be added 537 * 538 * @return the default value for type <code>T</code> 539 * 540 * @see Verifications#withCapture() 541 * @see Verifications#withCapture(Object) 542 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#withCapture" target="tutorial">Tutorial</a> 543 */ 544 @Nullable 545 protected final <T> T withCapture(@NonNull List<T> valueHolderForMultipleInvocations) { 546 addMatcher(new CaptureMatcher<>(valueHolderForMultipleInvocations)); 547 return null; 548 } 549 550 /** 551 * When passed as argument for an expectation, creates a new matcher that will check if the given value is 552 * {@link Object#equals(Object) equal} to the corresponding argument received by a matching invocation. 553 * <p> 554 * The matcher is added to the end of the list of argument matchers for the invocation being recorded/verified. It 555 * cannot be reused for a different parameter. 556 * <p> 557 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 558 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 559 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 560 * implicitly matched as if <code>withEqual(value)</code> had been used. In the special case of a varargs method, 561 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 562 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 563 * <p> 564 * Usually, this particular method should <em>not</em> be used. Instead, simply pass the desired argument value 565 * directly, without any matcher. Only when specifying values for a <em>varargs</em> method it's useful, and even 566 * then only when some other argument matcher is also used. 567 * 568 * @param <T> 569 * the generic type 570 * @param arg 571 * the expected argument value 572 * 573 * @return the given argument 574 * 575 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#withMethods" target="tutorial">Tutorial</a> 576 */ 577 @NonNull 578 protected final <T> T withEqual(@NonNull T arg) { 579 Map<Object, Object> instanceMap = currentPhase.getInstanceMap(); 580 addMatcher(new LenientEqualityMatcher(arg, instanceMap)); 581 return arg; 582 } 583 584 /** 585 * Same as {@link #withEqual(Object)}, but checking that a numeric invocation argument in the replay phase is 586 * sufficiently close to the given value. 587 * <p> 588 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 589 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 590 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 591 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 592 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 593 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 594 * 595 * @param value 596 * the center value for range comparison 597 * @param delta 598 * the tolerance around the center value, for a range of [value - delta, value + delta] 599 * 600 * @return the given <code>value</code> 601 * 602 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#withMethods" target="tutorial">Tutorial</a> 603 */ 604 protected final double withEqual(double value, double delta) { 605 addMatcher(new NumericEqualityMatcher(value, delta)); 606 return value; 607 } 608 609 /** 610 * Same as {@link #withEqual(Object)}, but checking that a numeric invocation argument in the replay phase is 611 * sufficiently close to the given value. 612 * <p> 613 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 614 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 615 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 616 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 617 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 618 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 619 * 620 * @param value 621 * the center value for range comparison 622 * @param delta 623 * the tolerance around the center value, for a range of [value - delta, value + delta] 624 * 625 * @return the given <code>value</code> 626 * 627 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#withMethods" target="tutorial">Tutorial</a> 628 */ 629 protected final float withEqual(float value, double delta) { 630 addMatcher(new NumericEqualityMatcher(value, delta)); 631 return value; 632 } 633 634 /** 635 * Same as {@link #withEqual(Object)}, but checking that an invocation argument in the replay phase is an instance 636 * of the same class as the given object. 637 * <p> 638 * Equivalent to a <code>withInstanceOf(object.getClass())</code> call, except that it returns <code>object</code> 639 * instead of <code>null</code>. 640 * <p> 641 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 642 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 643 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 644 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 645 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 646 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 647 * 648 * @param <T> 649 * the generic type 650 * @param object 651 * an instance of the desired class 652 * 653 * @return the given instance 654 * 655 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#withMethods" target="tutorial">Tutorial</a> 656 */ 657 @NonNull 658 protected final <T> T withInstanceLike(@NonNull T object) { 659 addMatcher(ClassMatcher.create(object.getClass())); 660 return object; 661 } 662 663 /** 664 * Same as {@link #withEqual(Object)}, but checking that an invocation argument in the replay phase is an instance 665 * of the given class. 666 * <p> 667 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 668 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 669 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 670 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 671 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 672 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 673 * 674 * @param <T> 675 * the generic type 676 * @param argClass 677 * the desired class 678 * 679 * @return always <code>null</code>; if you need a specific return value, use {@link #withInstanceLike(Object)} 680 * 681 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#withMethods" target="tutorial">Tutorial</a> 682 */ 683 @Nullable 684 protected final <T> T withInstanceOf(@NonNull Class<T> argClass) { 685 addMatcher(ClassMatcher.create(argClass)); 686 return null; 687 } 688 689 /** 690 * Same as {@link #withEqual(Object)}, but checking that the invocation argument in the replay phase is different 691 * from the given value. 692 * <p> 693 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 694 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 695 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 696 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 697 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 698 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 699 * 700 * @param <T> 701 * the generic type 702 * @param arg 703 * an arbitrary value, but different from the ones expected to occur during replay 704 * 705 * @return the given argument value 706 * 707 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#withMethods" target="tutorial">Tutorial</a> 708 */ 709 @NonNull 710 protected final <T> T withNotEqual(@NonNull T arg) { 711 addMatcher(new InequalityMatcher(arg)); 712 return arg; 713 } 714 715 /** 716 * Same as {@link #withEqual(Object)}, but checking that an invocation argument in the replay phase is 717 * <code>null</code>. 718 * <p> 719 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 720 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 721 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 722 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 723 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 724 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 725 * 726 * @param <T> 727 * the generic type 728 * 729 * @return always <code>null</code> 730 * 731 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#withMethods" target="tutorial">Tutorial</a> 732 */ 733 @Nullable 734 protected final <T> T withNull() { 735 addMatcher(NullityMatcher.INSTANCE); 736 return null; 737 } 738 739 /** 740 * Same as {@link #withEqual(Object)}, but checking that an invocation argument in the replay phase is not 741 * <code>null</code>. 742 * <p> 743 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 744 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 745 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 746 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 747 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 748 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 749 * 750 * @param <T> 751 * the generic type 752 * 753 * @return always <code>null</code> 754 * 755 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#withMethods" target="tutorial">Tutorial</a> 756 */ 757 @Nullable 758 protected final <T> T withNotNull() { 759 addMatcher(NonNullityMatcher.INSTANCE); 760 return null; 761 } 762 763 /** 764 * Same as {@link #withEqual(Object)}, but checking that an invocation argument in the replay phase is the exact 765 * same instance as the one in the recorded/verified invocation. 766 * <p> 767 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 768 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 769 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 770 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 771 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 772 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 773 * 774 * @param <T> 775 * the generic type 776 * @param object 777 * the desired instance 778 * 779 * @return the given object 780 * 781 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#withMethods" target="tutorial">Tutorial</a> 782 */ 783 @NonNull 784 protected final <T> T withSameInstance(@NonNull T object) { 785 addMatcher(new SamenessMatcher(object)); 786 return object; 787 } 788 789 /** 790 * Same as {@link #withEqual(Object)}, but checking that a textual invocation argument in the replay phase contains 791 * the given text as a substring. 792 * <p> 793 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 794 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 795 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 796 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 797 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 798 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 799 * 800 * @param <T> 801 * the generic type 802 * @param text 803 * an arbitrary non-null textual value 804 * 805 * @return the given text 806 * 807 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#withMethods" target="tutorial">Tutorial</a> 808 */ 809 @NonNull 810 protected final <T extends CharSequence> T withSubstring(@NonNull T text) { 811 addMatcher(new StringContainmentMatcher(text)); 812 return text; 813 } 814 815 /** 816 * Same as {@link #withEqual(Object)}, but checking that a textual invocation argument in the replay phase starts 817 * with the given text. 818 * <p> 819 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 820 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 821 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 822 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 823 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 824 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 825 * 826 * @param <T> 827 * the generic type 828 * @param text 829 * an arbitrary non-null textual value 830 * 831 * @return the given text 832 * 833 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#withMethods" target="tutorial">Tutorial</a> 834 */ 835 @NonNull 836 protected final <T extends CharSequence> T withPrefix(@NonNull T text) { 837 addMatcher(new StringPrefixMatcher(text)); 838 return text; 839 } 840 841 /** 842 * Same as {@link #withEqual(Object)}, but checking that a textual invocation argument in the replay phase ends with 843 * the given text. 844 * <p> 845 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 846 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 847 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 848 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 849 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 850 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 851 * 852 * @param <T> 853 * the generic type 854 * @param text 855 * an arbitrary non-null textual value 856 * 857 * @return the given text 858 * 859 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#withMethods" target="tutorial">Tutorial</a> 860 */ 861 @NonNull 862 protected final <T extends CharSequence> T withSuffix(@NonNull T text) { 863 addMatcher(new StringSuffixMatcher(text)); 864 return text; 865 } 866 867 /** 868 * Same as {@link #withEqual(Object)}, but checking that a textual invocation argument in the replay phase matches 869 * the given {@link Pattern regular expression}. 870 * <p> 871 * Note that this can be used for any string comparison, including case insensitive ones (with <code>"(?i)"</code> 872 * in the regex). 873 * <p> 874 * When an argument matcher is used for a regular (ie, non-varargs) parameter in a call to a mocked 875 * method/constructor, it's <em>not</em> necessary to also use matchers for the other parameters. So, it's valid to 876 * mix the use of matchers with given values. Any arguments given as literals, local variables, or fields will be 877 * implicitly matched as if {@link #withEqual(Object)} had been used. In the special case of a varargs method, 878 * however, this flexibility is not available: if a matcher is used for any regular parameter, or for any element in 879 * the varargs array, then a matcher <em>must</em> be used for every other parameter and varargs element. 880 * 881 * @param <T> 882 * the generic type 883 * @param regex 884 * an arbitrary (non-null) regular expression against which textual argument values will be matched 885 * 886 * @return the given regex 887 * 888 * @see Pattern#compile(String, int) 889 * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#withMethods" target="tutorial">Tutorial</a> 890 */ 891 @NonNull 892 protected final <T extends CharSequence> T withMatch(@NonNull T regex) { 893 addMatcher(new PatternMatcher(regex.toString())); 894 return regex; 895 } 896 }