Class MockitoMethodInvocationControl<T>
- java.lang.Object
-
- org.powermock.api.mockito.invocation.MockitoMethodInvocationControl<T>
-
- All Implemented Interfaces:
java.lang.reflect.InvocationHandler
,DefaultBehavior
,MethodInvocationControl
public class MockitoMethodInvocationControl<T> extends java.lang.Object implements MethodInvocationControl
A Mockito implementation of theMethodInvocationControl
interface.
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.Object
delegator
private java.util.Set<java.lang.reflect.Method>
mockedMethods
private MockHandlerAdaptor<T>
mockHandlerAdaptor
-
Constructor Summary
Constructors Constructor Description MockitoMethodInvocationControl(java.lang.Object delegator, T mockInstance, java.lang.reflect.Method... methodsToMock)
Creates a new instance with a delegator.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description MockHandlerAdaptor<T>
getMockHandlerAdaptor()
private boolean
hasBeenCaughtByMockitoProxy()
private boolean
hasDelegator()
java.lang.Object
invoke(java.lang.Object mock, java.lang.reflect.Method method, java.lang.Object[] arguments)
private boolean
isCanBeHandledByMockito(java.lang.reflect.Method method)
boolean
isMocked(java.lang.reflect.Method method)
Determine whether a certain method is mocked by this Invocation Control.java.lang.Object
replay(java.lang.Object... mocks)
Replay the given objects or classes.java.lang.Object
reset(java.lang.Object... mocks)
Reset the given objects or classes.private java.util.Set<java.lang.reflect.Method>
toSet(java.lang.reflect.Method... methods)
void
verifyNoMoreInteractions()
-
-
-
Field Detail
-
mockedMethods
private final java.util.Set<java.lang.reflect.Method> mockedMethods
-
delegator
private final java.lang.Object delegator
-
mockHandlerAdaptor
private final MockHandlerAdaptor<T> mockHandlerAdaptor
-
-
Constructor Detail
-
MockitoMethodInvocationControl
public MockitoMethodInvocationControl(java.lang.Object delegator, T mockInstance, java.lang.reflect.Method... methodsToMock)
Creates a new instance with a delegator. This delegator may benull
(if it is then no calls will be forwarded to this instance). If a delegator exists (i.e. not null) all non-mocked calls will be delegated to that instance.- Parameters:
delegator
- If the user spies on an instance the original instance must be injected here.mockInstance
- The actual mock instance. May benull
. Even though the mock instance may not be used it's needed to keep a reference to this object otherwise it may be garbage collected in some situations. For example when mocking static methods we don't return the mock object and thus it will be garbage collected (and thus the finalize method will be invoked which will be caught by the proxy and the test will fail because we haven't setup expectations for this method) because then that object has no reference. In order to avoid this we keep a reference to this instance here.methodsToMock
- The methods that are mocked for this instance. IfmethodsToMock
is null or empty, all methods for theinvocationHandler
are considered to be
-
-
Method Detail
-
isMocked
public boolean isMocked(java.lang.reflect.Method method)
Description copied from interface:MethodInvocationControl
Determine whether a certain method is mocked by this Invocation Control.- Specified by:
isMocked
in interfaceMethodInvocationControl
- Parameters:
method
- The method that should be checked.- Returns:
true
if the method is mocked,false
otherwise.
-
invoke
public java.lang.Object invoke(java.lang.Object mock, java.lang.reflect.Method method, java.lang.Object[] arguments) throws java.lang.Throwable
- Specified by:
invoke
in interfacejava.lang.reflect.InvocationHandler
- Throws:
java.lang.Throwable
-
isCanBeHandledByMockito
private boolean isCanBeHandledByMockito(java.lang.reflect.Method method)
-
hasBeenCaughtByMockitoProxy
private boolean hasBeenCaughtByMockitoProxy()
-
replay
public java.lang.Object replay(java.lang.Object... mocks)
Description copied from interface:DefaultBehavior
Replay the given objects or classes. May throw exception if replay is not needed or not supported.- Specified by:
replay
in interfaceDefaultBehavior
- Parameters:
mocks
- The object(s) to replay. May benull
.- Returns:
- the result of the replay (may be
null
).
-
reset
public java.lang.Object reset(java.lang.Object... mocks)
Description copied from interface:DefaultBehavior
Reset the given objects or classes. May throw exception if reset is not needed or not supported.- Specified by:
reset
in interfaceDefaultBehavior
- Parameters:
mocks
- The object(s) to replay. May benull
.- Returns:
- the result of the replay (may be
null
).
-
verifyNoMoreInteractions
public void verifyNoMoreInteractions()
-
toSet
private java.util.Set<java.lang.reflect.Method> toSet(java.lang.reflect.Method... methods)
-
hasDelegator
private boolean hasDelegator()
-
getMockHandlerAdaptor
public MockHandlerAdaptor<T> getMockHandlerAdaptor()
-
-