Class MockRepository
java.lang.Object
org.powermock.core.MockRepository
Hold mock objects that should be used instead of the concrete implementation.
Mock transformers may use this class to gather information on which classes
and methods that are mocked.
-
Field Summary
FieldsModifier and TypeFieldDescriptionSometimes mock frameworks needs to store additional state.Set of runnables that will be executed after the test (method) is completed.private static Map<Class<?>, MethodInvocationControl> Holds info about general method invocation mocks for classes.private static Map<Object, MethodInvocationControl> Holds info about general method invocation mocks for instances.private static Map<Method, InvocationHandler> Holds info about which methods that are proxied.private static Map<Class<?>, NewInvocationControl<?>> Holds info about which methods that should return a substitute/another instance instead of the default instance.private static final Set<Constructor<?>> Set of constructors that should be suppressed.Set of methods that should be suppressed.Set of field types that should always be suppressed regardless of instance.Set of methods that should be suppressed.Holds info about which class that should have their static initializers suppressed. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddAfterMethodRunner(Runnable runnable) Add aRunnablethat will be executed after each teststatic voidaddConstructorToSuppress(Constructor<?> constructor) Add a constructor to suppress.static voidaddFieldToSuppress(Field field) Add a field to suppress.static voidaddFieldTypeToSuppress(String fieldType) Add a field type to suppress.static voidaddMethodToSuppress(Method method) Add a method to suppress.static voidaddObjectsToAutomaticallyReplayAndVerify(Object... objects) Add classes that should be automatically replayed or verified.static voidaddSuppressStaticInitializer(String className) Add a fully qualified class name for a class that should have its static initializers suppressed.static voidclear()Clear all state of the mock repository except for static initializers.static <T> TgetAdditionalState(String key) Retrieve state based on the supplied key.static MethodInvocationControlgetInstanceMethodInvocationControl(Object instance) static InvocationHandlergetMethodProxy(Method method) static ObjectgetMethodToStub(Method method) static NewInvocationControl<?> getNewInstanceControl(Class<?> type) static MethodInvocationControlgetStaticMethodInvocationControl(Class<?> type) static booleanhasMethodProxy(Method method) static ObjectputAdditionalState(String key, Object value) When a mock framework API needs to store additional state not applicable for the other methods, it may use this method to do so.static MethodInvocationControlputInstanceMethodInvocationControl(Object instance, MethodInvocationControl invocationControl) static InvocationHandlerputMethodProxy(Method method, InvocationHandler invocationHandler) Set a proxy for a method.static ObjectputMethodToStub(Method method, Object value) Set a substitute return value for a method.static NewInvocationControl<?> putNewInstanceControl(Class<?> type, NewInvocationControl<?> control) static MethodInvocationControlputStaticMethodInvocationControl(Class<?> type, MethodInvocationControl invocationControl) static voidRemoves an object from the MockRepository if it exists.static Objectstatic MethodInvocationControlremoveClassMethodInvocationControl(Class<?> type) static MethodInvocationControlstatic InvocationHandlerremoveMethodProxy(Method method) static voidremoveSuppressStaticInitializer(String className) Remove a fully qualified class name for a class that should no longer have its static initializers suppressed.static booleanshouldStubMethod(Method method) static booleanshouldSuppressConstructor(Constructor<?> constructor) static booleanshouldSuppressField(Field field) static booleanshouldSuppressMethod(Method method, Class<?> objectType) static booleanshouldSuppressStaticInitializerFor(String className) Check whether or not a class with the fully qualified name should have its static initializers suppressed.
-
Field Details
-
objectsToAutomaticallyReplayAndVerify
-
newSubstitutions
-
classMocks
Holds info about general method invocation mocks for classes. -
instanceMocks
Holds info about general method invocation mocks for instances. -
substituteReturnValues
-
methodProxies
Holds info about which methods that are proxied. -
suppressStaticInitializers
-
additionalState
-
suppressConstructor
Set of constructors that should be suppressed. -
suppressMethod
-
suppressField
-
suppressFieldTypes
-
afterMethodRunners
-
-
Constructor Details
-
MockRepository
public MockRepository()
-
-
Method Details
-
clear
public static void clear()Clear all state of the mock repository except for static initializers. The reason for not clearing static initializers is that when running in a suite with many tests the clear method is invoked after each test. This means that before the test that needs to suppress the static initializer has been reach the state of the MockRepository would have been wiped out. This is generally not a problem because most state will be added again but suppression of static initializers are different because this state can only be set once per class per CL. That's why we cannot remove this state. -
remove
Removes an object from the MockRepository if it exists. -
getStaticMethodInvocationControl
-
putStaticMethodInvocationControl
public static MethodInvocationControl putStaticMethodInvocationControl(Class<?> type, MethodInvocationControl invocationControl) -
removeClassMethodInvocationControl
-
getInstanceMethodInvocationControl
-
putInstanceMethodInvocationControl
public static MethodInvocationControl putInstanceMethodInvocationControl(Object instance, MethodInvocationControl invocationControl) -
removeInstanceMethodInvocationControl
-
getNewInstanceControl
-
putNewInstanceControl
public static NewInvocationControl<?> putNewInstanceControl(Class<?> type, NewInvocationControl<?> control) -
addSuppressStaticInitializer
Add a fully qualified class name for a class that should have its static initializers suppressed.- Parameters:
className- The fully qualified class name for a class that should have its static initializers suppressed.
-
removeSuppressStaticInitializer
Remove a fully qualified class name for a class that should no longer have its static initializers suppressed.- Parameters:
className- The fully qualified class name for a class that should no longer have its static initializers suppressed.
-
shouldSuppressStaticInitializerFor
Check whether or not a class with the fully qualified name should have its static initializers suppressed.- Parameters:
className-trueif class with the fully qualified nameclassNameshould have its static initializers suppressed,falseotherwise.
-
getObjectsToAutomaticallyReplayAndVerify
-
addObjectsToAutomaticallyReplayAndVerify
Add classes that should be automatically replayed or verified. -
putAdditionalState
When a mock framework API needs to store additional state not applicable for the other methods, it may use this method to do so.- Parameters:
key- The key under which the value is stored.value- The value to store under the specified key.- Returns:
- The previous object under the specified key or
null.
-
removeAdditionalState
-
removeMethodProxy
-
getAdditionalState
Retrieve state based on the supplied key. -
addMethodToSuppress
Add a method to suppress.- Parameters:
method- The method to suppress.
-
addFieldToSuppress
Add a field to suppress.- Parameters:
field- The field to suppress.
-
addFieldTypeToSuppress
Add a field type to suppress. All fields of this type will be suppressed.- Parameters:
fieldType- The fully-qualified name to a type. All fields of this type will be suppressed.
-
addConstructorToSuppress
Add a constructor to suppress.- Parameters:
constructor- The constructor to suppress.
-
hasMethodProxy
- Returns:
trueif the method should be proxied.
-
shouldSuppressMethod
public static boolean shouldSuppressMethod(Method method, Class<?> objectType) throws ClassNotFoundException - Returns:
trueif the method should be suppressed.- Throws:
ClassNotFoundException
-
shouldSuppressField
- Returns:
trueif the field should be suppressed.
-
shouldSuppressConstructor
- Returns:
trueif the constructor should be suppressed.
-
shouldStubMethod
- Returns:
trueif the method has a substitute return value.
-
getMethodToStub
-
putMethodToStub
-
getMethodProxy
- Returns:
- The proxy for a particular method, may be
null.
-
putMethodProxy
Set a proxy for a method. Whenever this method is called the invocation handler will be invoked instead.- Returns:
- The method proxy if any.
-
addAfterMethodRunner
-