org.powermock.core
Class MockRepository

java.lang.Object
  extended by org.powermock.core.MockRepository

public class MockRepository
extends Object

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.


Constructor Summary
MockRepository()
           
 
Method Summary
static void addAfterMethodRunner(Runnable runnable)
          Add a Runnable that will be executed after each
static void addConstructorToSuppress(Constructor<?> constructor)
          Add a constructor to suppress.
static void addFieldToSuppress(Field field)
          Add a field to suppress.
static void addFieldTypeToSuppress(String fieldType)
          Add a field type to suppress.
static void addMethodToSuppress(Method method)
          Add a method to suppress.
static void addObjectsToAutomaticallyReplayAndVerify(Object... objects)
          Add classes that should be automatically replayed or verified.
static void addSuppressStaticInitializer(String className)
          Add a fully qualified class name for a class that should have its static initializers suppressed.
static void clear()
          Clear all state of the mock repository except for static initializers.
static
<T> T
getAdditionalState(String key)
          Retrieve state based on the supplied key.
static MethodInvocationControl getInstanceMethodInvocationControl(Object instance)
           
static InvocationHandler getMethodProxy(Method method)
           
static Object getMethodToStub(Method method)
           
static NewInvocationControl<?> getNewInstanceControl(Class<?> type)
           
static Set<Object> getObjectsToAutomaticallyReplayAndVerify()
           
static MethodInvocationControl getStaticMethodInvocationControl(Class<?> type)
           
static boolean hasMethodProxy(Method method)
           
static Object putAdditionalState(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 MethodInvocationControl putInstanceMethodInvocationControl(Object instance, MethodInvocationControl invocationControl)
           
static InvocationHandler putMethodProxy(Method method, InvocationHandler invocationHandler)
          Set a proxy for a method.
static Object putMethodToStub(Method method, Object value)
          Set a substitute return value for a method.
static NewInvocationControl<?> putNewInstanceControl(Class<?> type, NewInvocationControl<?> control)
           
static MethodInvocationControl putStaticMethodInvocationControl(Class<?> type, MethodInvocationControl invocationControl)
           
static void remove(Object mock)
          Removes an object from the MockRepository if it exists.
static Object removeAdditionalState(String key)
           
static MethodInvocationControl removeClassMethodInvocationControl(Class<?> type)
           
static MethodInvocationControl removeInstanceMethodInvocationControl(Class<?> type)
           
static InvocationHandler removeMethodProxy(Method method)
           
static void removeSuppressStaticInitializer(String className)
          Remove a fully qualified class name for a class that should no longer have its static initializers suppressed.
static boolean shouldStubMethod(Method method)
           
static boolean shouldSuppressConstructor(Constructor<?> constructor)
           
static boolean shouldSuppressField(Field field)
           
static boolean shouldSuppressMethod(Method method)
           
static boolean shouldSuppressStaticInitializerFor(String className)
          Check whether or not a class with the fully qualified name should have its static initializers suppressed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MockRepository

public MockRepository()
Method Detail

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

public static void remove(Object mock)
Removes an object from the MockRepository if it exists.


getStaticMethodInvocationControl

public static MethodInvocationControl getStaticMethodInvocationControl(Class<?> type)

putStaticMethodInvocationControl

public static MethodInvocationControl putStaticMethodInvocationControl(Class<?> type,
                                                                       MethodInvocationControl invocationControl)

removeClassMethodInvocationControl

public static MethodInvocationControl removeClassMethodInvocationControl(Class<?> type)

getInstanceMethodInvocationControl

public static MethodInvocationControl getInstanceMethodInvocationControl(Object instance)

putInstanceMethodInvocationControl

public static MethodInvocationControl putInstanceMethodInvocationControl(Object instance,
                                                                         MethodInvocationControl invocationControl)

removeInstanceMethodInvocationControl

public static MethodInvocationControl removeInstanceMethodInvocationControl(Class<?> type)

getNewInstanceControl

public static NewInvocationControl<?> getNewInstanceControl(Class<?> type)

putNewInstanceControl

public static NewInvocationControl<?> putNewInstanceControl(Class<?> type,
                                                            NewInvocationControl<?> control)

addSuppressStaticInitializer

public static void addSuppressStaticInitializer(String className)
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

public static void removeSuppressStaticInitializer(String className)
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

public static boolean shouldSuppressStaticInitializerFor(String className)
Check whether or not a class with the fully qualified name should have its static initializers suppressed.

Parameters:
className - true if class with the fully qualified name className should have its static initializers suppressed, false otherwise.

getObjectsToAutomaticallyReplayAndVerify

public static Set<Object> getObjectsToAutomaticallyReplayAndVerify()
Returns:
All classes that should be automatically replayed or verified.

addObjectsToAutomaticallyReplayAndVerify

public static void addObjectsToAutomaticallyReplayAndVerify(Object... objects)
Add classes that should be automatically replayed or verified.


putAdditionalState

public static Object putAdditionalState(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.

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

public static Object removeAdditionalState(String key)

removeMethodProxy

public static InvocationHandler removeMethodProxy(Method method)

getAdditionalState

public static <T> T getAdditionalState(String key)
Retrieve state based on the supplied key.


addMethodToSuppress

public static void addMethodToSuppress(Method method)
Add a method to suppress.

Parameters:
method - The method to suppress.

addFieldToSuppress

public static void addFieldToSuppress(Field field)
Add a field to suppress.

Parameters:
field - The field to suppress.

addFieldTypeToSuppress

public static void addFieldTypeToSuppress(String fieldType)
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

public static void addConstructorToSuppress(Constructor<?> constructor)
Add a constructor to suppress.

Parameters:
constructor - The constructor to suppress.

hasMethodProxy

public static boolean hasMethodProxy(Method method)
Returns:
true if the method should be proxied.

shouldSuppressMethod

public static boolean shouldSuppressMethod(Method method)
Returns:
true if the method should be suppressed.

shouldSuppressField

public static boolean shouldSuppressField(Field field)
Returns:
true if the field should be suppressed.

shouldSuppressConstructor

public static boolean shouldSuppressConstructor(Constructor<?> constructor)
Returns:
true if the constructor should be suppressed.

shouldStubMethod

public static boolean shouldStubMethod(Method method)
Returns:
true if the method has a substitute return value.

getMethodToStub

public static Object getMethodToStub(Method method)
Returns:
The substitute return value for a particular method, may be null.

putMethodToStub

public static Object putMethodToStub(Method method,
                                     Object value)
Set a substitute return value for a method. Whenever this method will be called the value will be returned instead.

Returns:
The previous substitute value if any.

getMethodProxy

public static InvocationHandler getMethodProxy(Method method)
Returns:
The proxy for a particular method, may be null.

putMethodProxy

public static InvocationHandler putMethodProxy(Method method,
                                               InvocationHandler invocationHandler)
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

public static void addAfterMethodRunner(Runnable runnable)
Add a Runnable that will be executed after each

Parameters:
runnable -


Copyright © 2007-2012. All Rights Reserved.