• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockito.invocation;
6 
7 import java.io.Serializable;
8 import java.lang.reflect.Method;
9 
10 import org.mockito.NotExtensible;
11 
12 /**
13  * An invocation on a mock.
14  *
15  * <p>
16  * A placeholder for mock, the method that was called and the arguments that were passed.
17  */
18 @NotExtensible
19 public interface InvocationOnMock extends Serializable {
20 
21     /**
22      * returns the mock object
23      *
24      * @return mock object
25      */
getMock()26     Object getMock();
27 
28     /**
29      * returns the method
30      *
31      * @return method
32      */
getMethod()33     Method getMethod();
34 
35     /**
36      * Returns arguments passed to the method.
37      *
38      * Vararg are expanded in this array.
39      *
40      * @return arguments
41      */
getArguments()42     Object[] getArguments();
43 
44     /**
45      * Returns casted argument at the given index.
46      *
47      * Can lookup in expanded arguments form {@link #getArguments()}.
48      *
49      * This method is preferred over {@link #getArgument(int, Class)} for readability. Please read
50      * the documentation of {@link #getArgument(int, Class)} for an overview of situations when
51      * that method is preferred over this one.
52      *
53      * @param index argument index
54      * @return casted argument at the given index
55      * @since 2.1.0
56      */
getArgument(int index)57     <T> T getArgument(int index);
58 
59     /**
60      * Returns casted argument at the given index. This method is analogous to
61      * {@link #getArgument(int)}, but is necessary to circumvent issues when dealing with generics.
62      *
63      * In general, {@link #getArgument(int)} is the appropriate function to use. This particular
64      * function is only necessary if you are doing one of the following things:
65      *
66      * <ol>
67      *  <li>You want to directly invoke a method on the result of {@link #getArgument(int)}.</li>
68      *  <li>You want to directly pass the result of the invocation into a function that accepts a generic parameter.</li>
69      * </ol>
70      *
71      * If you prefer to use {@link #getArgument(int)} instead, you can circumvent the compilation
72      * issues by storing the intermediate result into a local variable with the correct type.
73      *
74      * @param index argument index
75      * @param clazz class to cast the argument to
76      * @return casted argument at the given index
77      */
getArgument(int index, Class<T> clazz)78     <T> T getArgument(int index, Class<T> clazz);
79 
80     /**
81      * calls real method
82      * <p>
83      * <b>Warning:</b> depending on the real implementation it might throw exceptions
84      *
85      * @return whatever the real method returns / throws
86      * @throws Throwable in case real method throws
87      */
callRealMethod()88     Object callRealMethod() throws Throwable;
89 }
90