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