Package org.powermock.api.easymock
Class PowerMock
java.lang.Object
org.powermock.api.support.membermodification.MemberMatcher
org.powermock.api.support.membermodification.MemberModifier
org.powermock.api.easymock.PowerMock
PowerMock extends EasyMock functionality with several new features such as
mocking static and private methods, mocking new instances and more. Use
PowerMock instead of EasyMock where applicable.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
Clears the state in LastControl that deals with MocksControl. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
createMock
(Class<T> type) Creates a mock object that supports mocking of final and native methods.static <T> T
createMock
(Class<T> type, Object... constructorArguments) Creates a mock object that supports mocking of final and native methods and invokes a specific constructor based on the supplied argument values.static <T> T
createMock
(Class<T> type, Method... methods) Creates a mock object that supports mocking of final and native methods.static <T> T
createMock
(Class<T> type, org.easymock.ConstructorArgs constructorArgs, Method... methods) Creates a mock object that supports mocking of final and native methods and invokes a specific constructor.static <T> T
createMockAndExpectNew
(Class<T> type, Class<?>[] parameterTypes, Object... arguments) Convenience method for createMock followed by expectNew when PowerMock cannot determine which constructor to use automatically.static <T> T
createMockAndExpectNew
(Class<T> type, Object... arguments) Convenience method for createMock followed by expectNew.static <T> T
createNiceMock
(Class<T> type) Creates a nice mock object that supports mocking of final and native methods.static <T> T
createNiceMock
(Class<T> type, Object... constructorArguments) Creates a nice mock object that supports mocking of final and native methods and invokes a specific constructor based on the supplied argument values.static <T> T
createNiceMock
(Class<T> type, Method... methods) Creates a nice mock object that supports mocking of final and native methods.static <T> T
createNiceMock
(Class<T> type, org.easymock.ConstructorArgs constructorArgs, Method... methods) Creates a nice mock object that supports mocking of final and native methods and invokes a specific constructor.static <T> T
createNiceMockAndExpectNew
(Class<T> type, Class<?>[] parameterTypes, Object... arguments) Convenience method for createNiceMock followed by expectNew when PowerMock cannot determine which constructor to use automatically.static <T> T
createNiceMockAndExpectNew
(Class<T> type, Object... arguments) Convenience method for createNiceMock followed by expectNew.static <T> T
createNicePartialMock
(Class<T> type, Class<? super T> where, String... methodNames) A utility method that may be used to nicely mock several methods in an easy way (by just passing in the method names of the method you wish to mock).static <T> T
createNicePartialMock
(Class<T> type, String... methodNames) A utility method that may be used to nicely mock several methods in an easy way (by just passing in the method names of the method you wish to mock).static <T> T
createNicePartialMock
(Class<T> type, String[] methodNames, Object... constructorArguments) * A utility method that may be used to nicely mock several methods in an easy way (by just passing in the method names of the method you wish to mock).static <T> T
createNicePartialMock
(Class<T> type, String methodName, Class<?>[] methodParameterTypes, Object... constructorArguments) A utility method that may be used to nicely mock several methods in an easy way (by just passing in the method names of the method you wish to mock).static <T> T
createNicePartialMock
(Class<T> type, String methodName, Class<?>[] methodParameterTypes, Object[] constructorArguments, Class<?>[] constructorParameterTypes) A utility method that may be used to nicely mock several methods in an easy way (by just passing in the method names of the method you wish to mock).static <T> T
createNicePartialMock
(Class<T> type, String methodNameToMock, Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) Nicely mock a single specific method.static <T> T
createNicePartialMockAndInvokeDefaultConstructor
(Class<T> type, String... methodNames) A utility method that may be used to nicely mock several methods in an easy way (by just passing in the method names of the method you wish to mock).static <T> T
createNicePartialMockForAllMethodsExcept
(Class<T> type, String... methodNames) A utility method that may be used to specify several methods that should not be nicely mocked in an easy manner (by just passing in the method names of the method you wish not to mock).static <T> T
createNicePartialMockForAllMethodsExcept
(Class<T> type, String methodNameToExclude, Class<?> firstArgumentType, Class<?>... moreTypes) Mock all methods of a class except for a specific one nicely.static <T> T
createPartialMock
(Class<T> type, Class<? super T> where, String... methodNames) A utility method that may be used to mock several methods in an easy way (by just passing in the method names of the method you wish to mock).static <T> T
createPartialMock
(Class<T> type, String... methodNames) A utility method that may be used to mock several methods in an easy way (by just passing in the method names of the method you wish to mock).static <T> T
createPartialMock
(Class<T> type, String[] methodNames, Object... constructorArguments) A utility method that may be used to mock several methods in an easy way (by just passing in the method names of the method you wish to mock).static <T> T
createPartialMock
(Class<T> type, String methodName, Class<?>[] methodParameterTypes, Object... constructorArguments) A utility method that may be used to mock several methods in an easy way (by just passing in the method names of the method you wish to mock).static <T> T
createPartialMock
(Class<T> type, String methodName, Class<?>[] methodParameterTypes, Object[] constructorArguments, Class<?>[] constructorParameterTypes) A utility method that may be used to mock several methods in an easy way (by just passing in the method names of the method you wish to mock).static <T> T
createPartialMock
(Class<T> type, String methodNameToMock, Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) Mock a single specific method.static <T> T
createPartialMockAndInvokeDefaultConstructor
(Class<T> type, String... methodNames) A utility method that may be used to mock several methods in an easy way (by just passing in the method names of the method you wish to mock).static <T> T
createPartialMockForAllMethodsExcept
(Class<T> type, String... methodNames) A utility method that may be used to specify several methods that should not be mocked in an easy manner (by just passing in the method names of the method you wish not to mock).static <T> T
createPartialMockForAllMethodsExcept
(Class<T> type, String methodNameToExclude, Class<?> firstArgumentType, Class<?>... moreTypes) Mock all methods of a class except for a specific one.private static <T> Class<?>
createReplicaType
(Class<T> type, boolean isStatic, org.easymock.ConstructorArgs constructorArgs) static <T> T
createStrictMock
(Class<T> type) Creates a strict mock object that supports mocking of final and native methods.static <T> T
createStrictMock
(Class<T> type, Object... constructorArguments) Creates a strict mock object that supports mocking of final and native methods and invokes a specific constructor based on the supplied argument values.static <T> T
createStrictMock
(Class<T> type, Method... methods) Creates a strict mock object that supports mocking of final and native methods.static <T> T
createStrictMock
(Class<T> type, org.easymock.ConstructorArgs constructorArgs, Method... methods) Creates a strict mock object that supports mocking of final and native methods and invokes a specific constructor.static <T> T
createStrictMockAndExpectNew
(Class<T> type, Class<?>[] parameterTypes, Object... arguments) Convenience method for createStrictMock followed by expectNew when PowerMock cannot determine which constructor to use automatically.static <T> T
createStrictMockAndExpectNew
(Class<T> type, Object... arguments) Convenience method for createStrictMock followed by expectNew.static <T> T
createStrictPartialMock
(Class<T> type, Class<? super T> where, String... methodNames) A utility method that may be used to strictly mock several methods in an easy way (by just passing in the method names of the method you wish to mock).static <T> T
createStrictPartialMock
(Class<T> type, String... methodNames) A utility method that may be used to strictly mock several methods in an easy way (by just passing in the method names of the method you wish to mock).static <T> T
createStrictPartialMock
(Class<T> type, String[] methodNames, Object... constructorArguments) * A utility method that may be used to strictly mock several methods in an easy way (by just passing in the method names of the method you wish to mock).static <T> T
createStrictPartialMock
(Class<T> type, String methodName, Class<?>[] methodParameterTypes, Object... constructorArguments) A utility method that may be used to strictly mock several methods in an easy way (by just passing in the method names of the method you wish to mock).static <T> T
createStrictPartialMock
(Class<T> type, String methodName, Class<?>[] methodParameterTypes, Object[] constructorArguments, Class<?>[] constructorParameterTypes) A utility method that may be used to strictly mock several methods in an easy way (by just passing in the method names of the method you wish to mock).static <T> T
createStrictPartialMock
(Class<T> type, String methodNameToMock, Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) Strictly mock a single specific method.static <T> T
createStrictPartialMockAndInvokeDefaultConstructor
(Class<T> type, String... methodNames) A utility method that may be used to strictly mock several methods in an easy way (by just passing in the method names of the method you wish to mock).static <T> T
createStrictPartialMockForAllMethodsExcept
(Class<T> type, String... methodNames) A utility method that may be used to specify several methods that should not be strictly mocked in an easy manner (by just passing in the method names of the method you wish not to mock).static <T> T
createStrictPartialMockForAllMethodsExcept
(Class<T> type, String methodNameToExclude, Class<?> firstArgumentType, Class<?>... moreTypes) Mock all methods of a class except for a specific one strictly.private static <T> T
doCreateMock
(Class<T> type, org.easymock.ConstructorArgs constructorArgs, org.easymock.IMocksControl control, Method... methods) private static <T> org.easymock.IExpectationSetters<T>
doExpectNew
(Class<T> type, MockStrategy mockStrategy, Class<?>[] parameterTypes, Object... arguments) private static <T> org.easymock.IExpectationSetters<T>
doExpectPrivate
(Object instance, Method methodToExpect, Object... arguments) private static <T> T
doMock
(Class<T> type, boolean isStatic, MockStrategy mockStrategy, org.easymock.ConstructorArgs constructorArgs, Method... methods) (package private) static <T> T
doMockSpecific
(Class<T> type, MockStrategy mockStrategy, String[] methodNamesToMock, org.easymock.ConstructorArgs constructorArgs, Class<?>... argumentTypes) static org.easymock.IExpectationSetters<Object>
This method just delegates to EasyMock class extensionsEasyMock.expectLastCall()
method.static <T> org.easymock.IExpectationSetters<T>
Allows specifying expectations on new invocations.static <T> org.easymock.IExpectationSetters<T>
Allows specifying expectations on new invocations.static <T> org.easymock.IExpectationSetters<T>
Allows specifying expectations on new invocations for private member (inner) classes, local or anonymous classes.static <T> org.easymock.IExpectationSetters<T>
expectNiceNew
(Class<T> type, Class<?>[] parameterTypes, Object... arguments) Allows specifying expectations on new invocations.static <T> org.easymock.IExpectationSetters<T>
expectNiceNew
(Class<T> type, Object... arguments) Allows specifying expectations on new invocations.static <T> org.easymock.IExpectationSetters<T>
expectPrivate
(Class<?> clazz, Method method, Object... arguments) Used to specify expectations on private static methods.static <T> org.easymock.IExpectationSetters<T>
expectPrivate
(Object instance, Object... arguments) Used to specify expectations on methods without specifying a method name.static <T> org.easymock.IExpectationSetters<T>
expectPrivate
(Object instance, Method method, Object... arguments) Used to specify expectations on private methods.static <T> org.easymock.IExpectationSetters<T>
expectPrivate
(Object instance, String methodName, Class<?>[] parameterTypes, Object... arguments) Used to specify expectations on private methods.static <T> org.easymock.IExpectationSetters<T>
expectPrivate
(Object instance, String methodName, Class<?> where, Class<?>[] parameterTypes, Object... arguments) Used to specify expectations on methods using the method name at a specific place in the class hierarchy (specified by thewhere
parameter).static <T> org.easymock.IExpectationSetters<T>
expectPrivate
(Object instance, String methodName, Class<?> where, Object... arguments) Used to specify expectations on methods using the method name at a specific place in the class hierarchy (specified by thewhere
parameter).static <T> org.easymock.IExpectationSetters<T>
expectPrivate
(Object instance, String methodName, Object... arguments) Used to specify expectations on methods using the method name.static <T> org.easymock.IExpectationSetters<T>
expectStrictNew
(Class<T> type, Class<?>[] parameterTypes, Object... arguments) Allows specifying expectations on new invocations.static <T> org.easymock.IExpectationSetters<T>
expectStrictNew
(Class<T> type, Object... arguments) Allows specifying expectations on new invocations.private static boolean
isEasyMocked
(Object mock) Test if a object is a mock created by EasyMock or not.private static boolean
private static Class<?>[]
mergeArgumentTypes
(Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) static void
mockStatic
(Class<?> type) Enable static mocking for a class.static void
mockStatic
(Class<?> type, Method... methods) Enable static mocking for a class.static void
mockStaticNice
(Class<?> type) Enable nice static mocking for a class.static void
mockStaticNice
(Class<?> type, Method... methods) Enable nice static mocking for a class.static void
mockStaticPartial
(Class<?> clazz, String... methodNames) A utility method that may be used to mock several static methods in an easy way (by just passing in the method names of the method you wish to mock).static void
mockStaticPartial
(Class<?> clazz, String methodNameToMock, Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) Mock a single static method.static void
mockStaticPartialNice
(Class<?> clazz, String... methodNames) A utility method that may be used to mock several static methods (nice) in an easy way (by just passing in the method names of the method you wish to mock).static void
mockStaticPartialNice
(Class<?> clazz, String methodNameToMock, Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) Mock a single static method (nice).static void
mockStaticPartialStrict
(Class<?> clazz, String... methodNames) A utility method that may be used to mock several static methods (strict) in an easy way (by just passing in the method names of the method you wish to mock).static void
mockStaticPartialStrict
(Class<?> clazz, String methodNameToMock, Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) Mock a single static method (strict).static void
mockStaticStrict
(Class<?> type) Enable strict static mocking for a class.static void
mockStaticStrict
(Class<?> type, Method... methods) Enable strict static mocking for a class.static void
Sometimes it is useful to allow replay and verify on non-mocks.private static void
static void
Switches the mocks or classes to replay mode.static void
Replay all classes and mock objects known by PowerMock.static void
Reset a list of class mocks.static void
Reset a list of mock objects or classes.static void
Reset all classes and mock objects known by PowerMock.static void
suppressConstructor
(Class<?>... classes) Deprecated.static void
suppressConstructor
(Class<?> clazz, boolean excludePrivateConstructors) Deprecated.UseMemberModifier.suppress(Constructor[])
instead.static void
suppressConstructor
(Constructor<?>... constructors) Deprecated.UseMemberModifier.suppress(Constructor[])
instead.static void
suppressField
(Class<?>[] classes) Deprecated.UseMemberModifier.suppress(Field[])
instead.static void
suppressField
(Class<?> clazz, String... fieldNames) Deprecated.UseMemberModifier.suppress(Field)
instead.static void
suppressField
(Field... fields) Deprecated.UseMemberModifier.suppress(Field[])
instead.static void
suppressMethod
(Class<?>[] classes) Deprecated.UseMemberModifier.suppress(Method[])
instead.static void
suppressMethod
(Class<?> clazz, boolean excludePrivateMethods) Deprecated.UseMemberModifier.suppress(Method[])
instead.static void
suppressMethod
(Class<?> cls, Class<?>... additionalClasses) Deprecated.UseMemberModifier.suppress(Method[])
instead.static void
suppressMethod
(Class<?> clazz, String[] methodNames) Deprecated.UseMemberModifier.suppress(Method[])
instead.static void
suppressMethod
(Class<?> clazz, String methodName, Class<?>[] parameterTypes) Deprecated.UseMemberModifier.suppress(Method)
instead.static void
suppressMethod
(Class<?> clazz, String methodName, String... additionalMethodNames) Deprecated.UseMemberModifier.suppress(Method[])
instead.static void
suppressMethod
(Method... methods) Deprecated.UseMemberModifier.suppress(Method[])
instead.static void
suppressSpecificConstructor
(Class<?> clazz, Class<?>... parameterTypes) Deprecated.UseMemberModifier.suppress(Constructor)
instead.static void
Switches the mocks or classes to verify mode.static void
Verify all classes and mock objects known by PowerMock.private static void
verifyClass
(Class<?>... types) Note: doesn't clear PowerMock state.Methods inherited from class org.powermock.api.support.membermodification.MemberModifier
replace, stub, suppress, suppress, suppress, suppress, suppress, suppress, suppress
Methods inherited from class org.powermock.api.support.membermodification.MemberMatcher
constructor, constructor, constructors, constructorsDeclaredIn, defaultConstructorIn, everythingDeclaredIn, field, fields, fields, fields, fields, method, method, methods, methods, methods, methodsDeclaredIn
-
Field Details
-
NICE_REPLAY_AND_VERIFY_KEY
- See Also:
-
-
Constructor Details
-
PowerMock
public PowerMock()
-
-
Method Details
-
createMock
Creates a mock object that supports mocking of final and native methods.- Type Parameters:
T
- the type of the mock object- Parameters:
type
- the type of the mock objectmethods
- optionally what methods to mock- Returns:
- the mock object.
-
createMock
Creates a mock object that supports mocking of final and native methods.- Type Parameters:
T
- the type of the mock object- Parameters:
type
- the type of the mock object- Returns:
- the mock object.
-
createMock
public static <T> T createMock(Class<T> type, org.easymock.ConstructorArgs constructorArgs, Method... methods) Creates a mock object that supports mocking of final and native methods and invokes a specific constructor.- Type Parameters:
T
- the type of the mock object- Parameters:
type
- the type of the mock objectconstructorArgs
- The constructor arguments that will be used to invoke a special constructor.methods
- optionally what methods to mock- Returns:
- the mock object.
-
createMock
Creates a mock object that supports mocking of final and native methods and invokes a specific constructor based on the supplied argument values.- Type Parameters:
T
- the type of the mock object- Parameters:
type
- the type of the mock objectconstructorArguments
- The constructor arguments that will be used to invoke a certain constructor.- Returns:
- the mock object.
-
createStrictMock
Creates a strict mock object that supports mocking of final and native methods.- Type Parameters:
T
- the type of the mock object- Parameters:
type
- the type of the mock objectmethods
- optionally what methods to mock- Returns:
- the mock object.
-
createStrictMock
Creates a strict mock object that supports mocking of final and native methods.- Type Parameters:
T
- the type of the mock object- Parameters:
type
- the type of the mock object- Returns:
- the mock object.
-
createNiceMock
Creates a nice mock object that supports mocking of final and native methods.- Type Parameters:
T
- the type of the mock object- Parameters:
type
- the type of the mock objectmethods
- optionally what methods to mock- Returns:
- the mock object.
-
createNiceMock
Creates a nice mock object that supports mocking of final and native methods.- Type Parameters:
T
- the type of the mock object- Parameters:
type
- the type of the mock object- Returns:
- the mock object.
-
createStrictMock
public static <T> T createStrictMock(Class<T> type, org.easymock.ConstructorArgs constructorArgs, Method... methods) Creates a strict mock object that supports mocking of final and native methods and invokes a specific constructor.- Type Parameters:
T
- the type of the mock object- Parameters:
type
- the type of the mock objectconstructorArgs
- The constructor arguments that will be used to invoke a special constructor.methods
- optionally what methods to mock- Returns:
- the mock object.
-
createNiceMock
public static <T> T createNiceMock(Class<T> type, org.easymock.ConstructorArgs constructorArgs, Method... methods) Creates a nice mock object that supports mocking of final and native methods and invokes a specific constructor.- Type Parameters:
T
- the type of the mock object- Parameters:
type
- the type of the mock objectconstructorArgs
- The constructor arguments that will be used to invoke a special constructor.methods
- optionally what methods to mock- Returns:
- the mock object.
-
createStrictMock
Creates a strict mock object that supports mocking of final and native methods and invokes a specific constructor based on the supplied argument values.- Type Parameters:
T
- the type of the mock object- Parameters:
type
- the type of the mock objectconstructorArguments
- The constructor arguments that will be used to invoke a certain constructor.- Returns:
- the mock object.
-
createNiceMock
Creates a nice mock object that supports mocking of final and native methods and invokes a specific constructor based on the supplied argument values.- Type Parameters:
T
- the type of the mock object- Parameters:
type
- the type of the mock objectconstructorArguments
- The constructor arguments that will be used to invoke a certain constructor.- Returns:
- the mock object.
-
mockStatic
Enable static mocking for a class.- Parameters:
type
- the class to enable static mockingmethods
- optionally what methods to mock
-
mockStatic
Enable static mocking for a class.- Parameters:
type
- the class to enable static mocking
-
mockStaticStrict
Enable strict static mocking for a class.- Parameters:
type
- the class to enable static mockingmethods
- optionally what methods to mock
-
mockStaticStrict
Enable strict static mocking for a class.- Parameters:
type
- the class to enable static mocking
-
mockStaticNice
Enable nice static mocking for a class.- Parameters:
type
- the class to enable static mockingmethods
- optionally what methods to mock
-
mockStaticNice
Enable nice static mocking for a class.- Parameters:
type
- the class to enable static mocking
-
createPartialMockForAllMethodsExcept
A utility method that may be used to specify several methods that should not be mocked in an easy manner (by just passing in the method names of the method you wish not to mock). Note that you cannot uniquely specify a method to exclude using this method if there are several methods with the same name intype
. This method will mock ALL methods that doesn't match the supplied name(s) regardless of parameter types and signature. If this is not the case you should fall-back on using thecreateMock(Class, Method...)
method instead.- Type Parameters:
T
- The type of the mock.- Parameters:
type
- The type that'll be used to create a mock instance.methodNames
- The names of the methods that should be mocked. Ifnull
, then this method will have the same effect as just callingcreateMock(Class, Method...)
with the second parameter asnew Method[0]
(i.e. all methods in that class will be mocked).- Returns:
- A mock object of type
.
-
createNicePartialMockForAllMethodsExcept
A utility method that may be used to specify several methods that should not be nicely mocked in an easy manner (by just passing in the method names of the method you wish not to mock). Note that you cannot uniquely specify a method to exclude using this method if there are several methods with the same name intype
. This method will mock ALL methods that doesn't match the supplied name(s) regardless of parameter types and signature. If this is not the case you should fall-back on using thecreateMock(Class, Method...)
method instead.- Type Parameters:
T
- The type of the mock.- Parameters:
type
- The type that'll be used to create a mock instance.methodNames
- The names of the methods that should be mocked. Ifnull
, then this method will have the same effect as just callingcreateMock(Class, Method...)
with the second parameter asnew Method[0]
(i.e. all methods in that class will be mocked).- Returns:
- A mock object of type
.
-
createStrictPartialMockForAllMethodsExcept
public static <T> T createStrictPartialMockForAllMethodsExcept(Class<T> type, String... methodNames) A utility method that may be used to specify several methods that should not be strictly mocked in an easy manner (by just passing in the method names of the method you wish not to mock). Note that you cannot uniquely specify a method to exclude using this method if there are several methods with the same name intype
. This method will mock ALL methods that doesn't match the supplied name(s) regardless of parameter types and signature. If this is not the case you should fall-back on using thecreateMock(Class, Method...)
method instead.- Type Parameters:
T
- The type of the mock.- Parameters:
type
- The type that'll be used to create a mock instance.methodNames
- The names of the methods that should be mocked. Ifnull
, then this method will have the same effect as just callingcreateMock(Class, Method...)
with the second parameter asnew Method[0]
(i.e. all methods in that class will be mocked).- Returns:
- A mock object of type
.
-
createPartialMockForAllMethodsExcept
public static <T> T createPartialMockForAllMethodsExcept(Class<T> type, String methodNameToExclude, Class<?> firstArgumentType, Class<?>... moreTypes) Mock all methods of a class except for a specific one. Use this method only if you have several overloaded methods.- Type Parameters:
T
- The type of the mock.- Parameters:
type
- The type that'll be used to create a mock instance.methodNameToExclude
- The name of the method not to mock.firstArgumentType
- The type of the first parameter of the method not to mockmoreTypes
- Optionally more parameter types that defines the method. Note that this is only needed to separate overloaded methods.- Returns:
- A mock object of type
.
-
createNicePartialMockForAllMethodsExcept
public static <T> T createNicePartialMockForAllMethodsExcept(Class<T> type, String methodNameToExclude, Class<?> firstArgumentType, Class<?>... moreTypes) Mock all methods of a class except for a specific one nicely. Use this method only if you have several overloaded methods.- Type Parameters:
T
- The type of the mock.- Parameters:
type
- The type that'll be used to create a mock instance.methodNameToExclude
- The name of the method not to mock.firstArgumentType
- The type of the first parameter of the method not to mockmoreTypes
- Optionally more parameter types that defines the method. Note that this is only needed to separate overloaded methods.- Returns:
- A mock object of type
.
-
createStrictPartialMockForAllMethodsExcept
public static <T> T createStrictPartialMockForAllMethodsExcept(Class<T> type, String methodNameToExclude, Class<?> firstArgumentType, Class<?>... moreTypes) Mock all methods of a class except for a specific one strictly. Use this method only if you have several overloaded methods.- Type Parameters:
T
- The type of the mock.- Parameters:
type
- The type that'll be used to create a mock instance.methodNameToExclude
- The name of the method not to mock.firstArgumentType
- The type of the first parameter of the method not to mockmoreTypes
- Optionally more parameter types that defines the method. Note that this is only needed to separate overloaded methods.- Returns:
- A mock object of type
.
-
createPartialMock
public static <T> T createPartialMock(Class<T> type, String methodNameToMock, Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) Mock a single specific method. Use this to handle overloaded methods.- Type Parameters:
T
- The type of the mock.- Parameters:
type
- The type that'll be used to create a mock instance.methodNameToMock
- The name of the method to mockfirstArgumentType
- The type of the first parameter of the method to mockadditionalArgumentTypes
- Optionally more parameter types that defines the method. Note that this is only needed to separate overloaded methods.- Returns:
- A mock object of type
.
-
createStrictPartialMock
public static <T> T createStrictPartialMock(Class<T> type, String methodNameToMock, Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) Strictly mock a single specific method. Use this to handle overloaded methods.- Type Parameters:
T
- The type of the mock.- Parameters:
type
- The type that'll be used to create a mock instance.methodNameToMock
- The name of the method to mockfirstArgumentType
- The type of the first parameter of the method to mockadditionalArgumentTypes
- Optionally more parameter types that defines the method. Note that this is only needed to separate overloaded methods.- Returns:
- A mock object of type
.
-
createNicePartialMock
public static <T> T createNicePartialMock(Class<T> type, String methodNameToMock, Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) Nicely mock a single specific method. Use this to handle overloaded methods.- Type Parameters:
T
- The type of the mock.- Parameters:
type
- The type that'll be used to create a mock instance.methodNameToMock
- The name of the method to mockfirstArgumentType
- The type of the first parameter of the method to mockadditionalArgumentTypes
- Optionally more parameter types that defines the method. Note that this is only needed to separate overloaded methods.- Returns:
- A mock object of type
.
-
mockStaticPartial
public static void mockStaticPartial(Class<?> clazz, String methodNameToMock, Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) Mock a single static method.- Parameters:
clazz
- The class where the method is specified in.methodNameToMock
- The first argumentfirstArgumentType
- The first argument type.additionalArgumentTypes
- Optional additional argument types.
-
mockStaticPartialStrict
public static void mockStaticPartialStrict(Class<?> clazz, String methodNameToMock, Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) Mock a single static method (strict).- Parameters:
clazz
- The class where the method is specified in.methodNameToMock
- The first argumentfirstArgumentType
- The first argument type.additionalArgumentTypes
- Optional additional argument types.
-
mockStaticPartialNice
public static void mockStaticPartialNice(Class<?> clazz, String methodNameToMock, Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) Mock a single static method (nice).- Parameters:
clazz
- The class where the method is specified in.methodNameToMock
- The first argumentfirstArgumentType
- The first argument type.additionalArgumentTypes
- Optional additional argument types.
-
mockStaticPartial
A utility method that may be used to mock several static methods in an easy way (by just passing in the method names of the method you wish to mock). Note that you cannot uniquely specify a method to mock using this method if there are several methods with the same name intype
. This method will mock ALL methods that match the supplied name regardless of parameter types and signature. If this is the case you should fall-back on using themockStatic(Class, Method...)
method instead.- Parameters:
clazz
- The class that contains the static methods that should be mocked.methodNames
- The names of the methods that should be mocked. Ifnull
, then this method will have the same effect as just callingmockStatic(Class, Method...)
with the second parameter asnew Method[0]
(i.e. all methods in that class will be mocked).
-
mockStaticPartialStrict
A utility method that may be used to mock several static methods (strict) in an easy way (by just passing in the method names of the method you wish to mock). Note that you cannot uniquely specify a method to mock using this method if there are several methods with the same name intype
. This method will mock ALL methods that match the supplied name regardless of parameter types and signature. If this is the case you should fall-back on using themockStaticStrict(Class, Method...)
method instead.- Parameters:
clazz
- The class that contains the static methods that should be mocked.methodNames
- The names of the methods that should be mocked. Ifnull
, then this method will have the same effect as just callingmockStatic(Class, Method...)
with the second parameter asnew Method[0]
(i.e. all methods in that class will be mocked).
-
mockStaticPartialNice
A utility method that may be used to mock several static methods (nice) in an easy way (by just passing in the method names of the method you wish to mock). Note that you cannot uniquely specify a method to mock using this method if there are several methods with the same name intype
. This method will mock ALL methods that match the supplied name regardless of parameter types and signature. If this is the case you should fall-back on using themockStaticStrict(Class, Method...)
method instead.- Parameters:
clazz
- The class that contains the static methods that should be mocked.methodNames
- The names of the methods that should be mocked. Ifnull
, then this method will have the same effect as just callingmockStatic(Class, Method...)
with the second parameter asnew Method[0]
(i.e. all methods in that class will be mocked).
-
doMockSpecific
static <T> T doMockSpecific(Class<T> type, MockStrategy mockStrategy, String[] methodNamesToMock, org.easymock.ConstructorArgs constructorArgs, Class<?>... argumentTypes) -
createPartialMock
A utility method that may be used to mock several methods in an easy way (by just passing in the method names of the method you wish to mock). Note that you cannot uniquely specify a method to mock using this method if there are several methods with the same name intype
. This method will mock ALL methods that match the supplied name regardless of parameter types and signature. If this is the case you should fall-back on using thecreateMock(Class, Method...)
method instead.- Type Parameters:
T
- The type of the mock.- Parameters:
type
- The type that'll be used to create a mock instance.methodNames
- The names of the methods that should be mocked. Ifnull
, then this method will have the same effect as just callingcreateMock(Class, Method...)
with the second parameter asnew Method[0]
(i.e. all methods in that class will be mocked).- Returns:
- A mock object of type
.
-
createPartialMock
A utility method that may be used to mock several methods in an easy way (by just passing in the method names of the method you wish to mock). Note that you cannot uniquely specify a method to mock using this method if there are several methods with the same name intype
. This method will mock ALL methods that match the supplied name regardless of parameter types and signature. If this is the case you should fall-back on using thecreateMock(Class, Method...)
method instead. With this method you can specify where the class hierarchy the methods are located. This is useful in, for example, situations where class A extends B and both have a method called "mockMe" (A overrides B's mockMe method) and you like to specify the only the "mockMe" method in B should be mocked. "mockMe" in A should be left intact. In this case you should do:A tested = createPartialMock(A.class, B.class, "mockMe");
- Type Parameters:
T
- The type of the mock.- Parameters:
type
- The type that'll be used to create a mock instance.where
- Where in the class hierarchy the methods resides.methodNames
- The names of the methods that should be mocked. Ifnull
, then this method will have the same effect as just callingcreateMock(Class, Method...)
with the second parameter asnew Method[0]
(i.e. all methods in that class will be mocked).- Returns:
- A mock object of type
.
-
createStrictPartialMock
A utility method that may be used to strictly mock several methods in an easy way (by just passing in the method names of the method you wish to mock). Note that you cannot uniquely specify a method to mock using this method if there are several methods with the same name intype
. This method will mock ALL methods that match the supplied name regardless of parameter types and signature. If this is the case you should fall-back on using thecreateMock(Class, Method...)
method instead.- Type Parameters:
T
- The type of the mock.- Parameters:
type
- The type that'll be used to create a mock instance.methodNames
- The names of the methods that should be mocked. Ifnull
, then this method will have the same effect as just callingcreateMock(Class, Method...)
with the second parameter asnew Method[0]
(i.e. all methods in that class will be mocked).- Returns:
- A mock object of type
.
-
createStrictPartialMock
public static <T> T createStrictPartialMock(Class<T> type, Class<? super T> where, String... methodNames) A utility method that may be used to strictly mock several methods in an easy way (by just passing in the method names of the method you wish to mock). Note that you cannot uniquely specify a method to mock using this method if there are several methods with the same name intype
. This method will mock ALL methods that match the supplied name regardless of parameter types and signature. If this is the case you should fall-back on using thecreateMock(Class, Method...)
method instead. With this method you can specify where the class hierarchy the methods are located. This is useful in, for example, situations where class A extends B and both have a method called "mockMe" (A overrides B's mockMe method) and you like to specify the only the "mockMe" method in B should be mocked. "mockMe" in A should be left intact. In this case you should do:A tested = createPartialMockStrict(A.class, B.class, "mockMe");
- Type Parameters:
T
- The type of the mock.- Parameters:
type
- The type that'll be used to create a mock instance.where
- Where in the class hierarchy the methods resides.methodNames
- The names of the methods that should be mocked. Ifnull
, then this method will have the same effect as just callingcreateMock(Class, Method...)
with the second parameter asnew Method[0]
(i.e. all methods in that class will be mocked).- Returns:
- A mock object of type
.
-
createNicePartialMock
A utility method that may be used to nicely mock several methods in an easy way (by just passing in the method names of the method you wish to mock). Note that you cannot uniquely specify a method to mock using this method if there are several methods with the same name intype
. This method will mock ALL methods that match the supplied name regardless of parameter types and signature. If this is the case you should fall-back on using thecreateMock(Class, Method...)
method instead.- Type Parameters:
T
- The type of the mock.- Parameters:
type
- The type that'll be used to create a mock instance.methodNames
- The names of the methods that should be mocked. Ifnull
, then this method will have the same effect as just callingcreateMock(Class, Method...)
with the second parameter asnew Method[0]
(i.e. all methods in that class will be mocked).- Returns:
- A mock object of type
.
-
createNicePartialMock
public static <T> T createNicePartialMock(Class<T> type, Class<? super T> where, String... methodNames) A utility method that may be used to nicely mock several methods in an easy way (by just passing in the method names of the method you wish to mock). Note that you cannot uniquely specify a method to mock using this method if there are several methods with the same name intype
. This method will mock ALL methods that match the supplied name regardless of parameter types and signature. If this is the case you should fall-back on using thecreateMock(Class, Method...)
method instead. With this method you can specify where the class hierarchy the methods are located. This is useful in, for example, situations where class A extends B and both have a method called "mockMe" (A overrides B's mockMe method) and you like to specify the only the "mockMe" method in B should be mocked. "mockMe" in A should be left intact. In this case you should do:A tested = createPartialMockNice(A.class, B.class, "mockMe");
- Type Parameters:
T
- The type of the mock.- Parameters:
type
- The type that'll be used to create a mock instance.where
- Where in the class hierarchy the methods resides.methodNames
- The names of the methods that should be mocked. Ifnull
, then this method will have the same effect as just callingcreateMock(Class, Method...)
with the second parameter asnew Method[0]
(i.e. all methods in that class will be mocked).- Returns:
- A mock object of type
.
-
createPartialMockAndInvokeDefaultConstructor
public static <T> T createPartialMockAndInvokeDefaultConstructor(Class<T> type, String... methodNames) throws Exception A utility method that may be used to mock several methods in an easy way (by just passing in the method names of the method you wish to mock). The mock object created will support mocking of final methods and invokes the default constructor (even if it's private).- Type Parameters:
T
- the type of the mock object- Parameters:
type
- the type of the mock objectmethodNames
- The names of the methods that should be mocked. Ifnull
, then this method will have the same effect as just callingcreateMock(Class, Method...)
with the second parameter asnew Method[0]
(i.e. all methods in that class will be mocked).- Returns:
- the mock object.
- Throws:
Exception
-
createNicePartialMockAndInvokeDefaultConstructor
public static <T> T createNicePartialMockAndInvokeDefaultConstructor(Class<T> type, String... methodNames) throws Exception A utility method that may be used to nicely mock several methods in an easy way (by just passing in the method names of the method you wish to mock). The mock object created will support mocking of final methods and invokes the default constructor (even if it's private).- Type Parameters:
T
- the type of the mock object- Parameters:
type
- the type of the mock objectmethodNames
- The names of the methods that should be mocked. Ifnull
, then this method will have the same effect as just callingcreateMock(Class, Method...)
with the second parameter asnew Method[0]
(i.e. all methods in that class will be mocked).- Returns:
- the mock object.
- Throws:
Exception
-
createStrictPartialMockAndInvokeDefaultConstructor
public static <T> T createStrictPartialMockAndInvokeDefaultConstructor(Class<T> type, String... methodNames) throws Exception A utility method that may be used to strictly mock several methods in an easy way (by just passing in the method names of the method you wish to mock). The mock object created will support mocking of final methods and invokes the default constructor (even if it's private).- Type Parameters:
T
- the type of the mock object- Parameters:
type
- the type of the mock objectmethodNames
- The names of the methods that should be mocked. Ifnull
, then this method will have the same effect as just callingcreateMock(Class, Method...)
with the second parameter asnew Method[0]
(i.e. all methods in that class will be mocked).- Returns:
- the mock object.
- Throws:
Exception
-
createPartialMock
public static <T> T createPartialMock(Class<T> type, String[] methodNames, Object... constructorArguments) A utility method that may be used to mock several methods in an easy way (by just passing in the method names of the method you wish to mock). The mock object created will support mocking of final and native methods and invokes a specific constructor based on the supplied argument values.- Type Parameters:
T
- the type of the mock object- Parameters:
type
- the type of the mock objectmethodNames
- The names of the methods that should be mocked. Ifnull
, then this method will have the same effect as just callingcreateMock(Class, Method...)
with the second parameter asnew Method[0]
(i.e. all methods in that class will be mocked).constructorArguments
- The constructor arguments that will be used to invoke a certain constructor. (optional)- Returns:
- the mock object.
-
createStrictPartialMock
public static <T> T createStrictPartialMock(Class<T> type, String[] methodNames, Object... constructorArguments) * A utility method that may be used to strictly mock several methods in an easy way (by just passing in the method names of the method you wish to mock). The mock object created will support mocking of final and native methods and invokes a specific constructor based on the supplied argument values.- Type Parameters:
T
- the type of the mock object- Parameters:
type
- the type of the mock objectmethodNames
- The names of the methods that should be mocked. Ifnull
, then this method will have the same effect as just callingcreateMock(Class, Method...)
with the second parameter asnew Method[0]
(i.e. all methods in that class will be mocked).constructorArguments
- The constructor arguments that will be used to invoke a certain constructor. (optional)- Returns:
- the mock object.
-
createNicePartialMock
public static <T> T createNicePartialMock(Class<T> type, String[] methodNames, Object... constructorArguments) * A utility method that may be used to nicely mock several methods in an easy way (by just passing in the method names of the method you wish to mock). The mock object created will support mocking of final and native methods and invokes a specific constructor based on the supplied argument values.- Type Parameters:
T
- the type of the mock object- Parameters:
type
- the type of the mock objectmethodNames
- The names of the methods that should be mocked. Ifnull
, then this method will have the same effect as just callingcreateMock(Class, Method...)
with the second parameter asnew Method[0]
(i.e. all methods in that class will be mocked).constructorArguments
- The constructor arguments that will be used to invoke a certain constructor. (optional)- Returns:
- the mock object.
-
createPartialMock
public static <T> T createPartialMock(Class<T> type, String methodName, Class<?>[] methodParameterTypes, Object... constructorArguments) A utility method that may be used to mock several methods in an easy way (by just passing in the method names of the method you wish to mock). Use this to handle overloaded methods. The mock object created will support mocking of final and native methods and invokes a specific constructor based on the supplied argument values.- Type Parameters:
T
- the type of the mock object- Parameters:
type
- the type of the mock objectmethodName
- The names of the methods that should be mocked. Ifnull
, then this method will have the same effect as just callingcreateMock(Class, Method...)
with the second parameter asnew Method[0]
(i.e. all methods in that class will be mocked).methodParameterTypes
- Parameter types that defines the method. Note that this is only needed to separate overloaded methods.constructorArguments
- The constructor arguments that will be used to invoke a certain constructor. (optional)- Returns:
- the mock object.
-
createStrictPartialMock
public static <T> T createStrictPartialMock(Class<T> type, String methodName, Class<?>[] methodParameterTypes, Object... constructorArguments) A utility method that may be used to strictly mock several methods in an easy way (by just passing in the method names of the method you wish to mock). Use this to handle overloaded methods. The mock object created will support mocking of final and native methods and invokes a specific constructor based on the supplied argument values.- Type Parameters:
T
- the type of the mock object- Parameters:
type
- the type of the mock objectmethodName
- The names of the methods that should be mocked. Ifnull
, then this method will have the same effect as just callingcreateMock(Class, Method...)
with the second parameter asnew Method[0]
(i.e. all methods in that class will be mocked).methodParameterTypes
- Parameter types that defines the method. Note that this is only needed to separate overloaded methods.constructorArguments
- The constructor arguments that will be used to invoke a certain constructor. (optional)- Returns:
- the mock object.
-
createNicePartialMock
public static <T> T createNicePartialMock(Class<T> type, String methodName, Class<?>[] methodParameterTypes, Object... constructorArguments) A utility method that may be used to nicely mock several methods in an easy way (by just passing in the method names of the method you wish to mock). Use this to handle overloaded methods. The mock object created will support mocking of final and native methods and invokes a specific constructor based on the supplied argument values.- Type Parameters:
T
- the type of the mock object- Parameters:
type
- the type of the mock objectmethodName
- The names of the methods that should be mocked. Ifnull
, then this method will have the same effect as just callingcreateMock(Class, Method...)
with the second parameter asnew Method[0]
(i.e. all methods in that class will be mocked).methodParameterTypes
- Parameter types that defines the method. Note that this is only needed to separate overloaded methods.constructorArguments
- The constructor arguments that will be used to invoke a certain constructor. (optional)- Returns:
- the mock object.
-
createPartialMock
public static <T> T createPartialMock(Class<T> type, String methodName, Class<?>[] methodParameterTypes, Object[] constructorArguments, Class<?>[] constructorParameterTypes) A utility method that may be used to mock several methods in an easy way (by just passing in the method names of the method you wish to mock). Use this to handle overloaded methods and overloaded constructors. The mock object created will support mocking of final and native methods and invokes a specific constructor based on the supplied argument values.- Type Parameters:
T
- the type of the mock object- Parameters:
type
- the type of the mock objectmethodName
- The names of the methods that should be mocked. Ifnull
, then this method will have the same effect as just callingcreateMock(Class, Method...)
with the second parameter asnew Method[0]
(i.e. all methods in that class will be mocked).methodParameterTypes
- Parameter types that defines the method. Note that this is only needed to separate overloaded methods.constructorArguments
- The constructor arguments that will be used to invoke a certain constructor.constructorParameterTypes
- Parameter types that defines the constructor that should be invoked. Note that this is only needed to separate overloaded constructors.- Returns:
- the mock object.
-
createStrictPartialMock
public static <T> T createStrictPartialMock(Class<T> type, String methodName, Class<?>[] methodParameterTypes, Object[] constructorArguments, Class<?>[] constructorParameterTypes) A utility method that may be used to strictly mock several methods in an easy way (by just passing in the method names of the method you wish to mock). Use this to handle overloaded methods and overloaded constructors. The mock object created will support mocking of final and native methods and invokes a specific constructor based on the supplied argument values.- Type Parameters:
T
- the type of the mock object- Parameters:
type
- the type of the mock objectmethodName
- The names of the methods that should be mocked. Ifnull
, then this method will have the same effect as just callingcreateMock(Class, Method...)
with the second parameter asnew Method[0]
(i.e. all methods in that class will be mocked).methodParameterTypes
- Parameter types that defines the method. Note that this is only needed to separate overloaded methods.constructorArguments
- The constructor arguments that will be used to invoke a certain constructor.constructorParameterTypes
- Parameter types that defines the constructor that should be invoked. Note that this is only needed to separate overloaded constructors.- Returns:
- the mock object.
-
createNicePartialMock
public static <T> T createNicePartialMock(Class<T> type, String methodName, Class<?>[] methodParameterTypes, Object[] constructorArguments, Class<?>[] constructorParameterTypes) A utility method that may be used to nicely mock several methods in an easy way (by just passing in the method names of the method you wish to mock). Use this to handle overloaded methods and overloaded constructors. The mock object created will support mocking of final and native methods and invokes a specific constructor based on the supplied argument values.- Type Parameters:
T
- the type of the mock object- Parameters:
type
- the type of the mock objectmethodName
- The names of the methods that should be mocked. Ifnull
, then this method will have the same effect as just callingcreateMock(Class, Method...)
with the second parameter asnew Method[0]
(i.e. all methods in that class will be mocked).methodParameterTypes
- Parameter types that defines the method. Note that this is only needed to separate overloaded methods.constructorArguments
- The constructor arguments that will be used to invoke a certain constructor.constructorParameterTypes
- Parameter types that defines the constructor that should be invoked. Note that this is only needed to separate overloaded constructors.- Returns:
- the mock object.
-
expectPrivate
public static <T> org.easymock.IExpectationSetters<T> expectPrivate(Class<?> clazz, Method method, Object... arguments) throws Exception Used to specify expectations on private static methods. If possible use variant with only method name.- Throws:
Exception
-
expectPrivate
public static <T> org.easymock.IExpectationSetters<T> expectPrivate(Object instance, Method method, Object... arguments) throws Exception Used to specify expectations on private methods. If possible use variant with only method name.- Throws:
Exception
-
expectPrivate
public static <T> org.easymock.IExpectationSetters<T> expectPrivate(Object instance, String methodName, Class<?>[] parameterTypes, Object... arguments) throws Exception Used to specify expectations on private methods. Use this method to handle overloaded methods.- Throws:
Exception
-
expectPrivate
public static <T> org.easymock.IExpectationSetters<T> expectPrivate(Object instance, String methodName, Object... arguments) throws Exception Used to specify expectations on methods using the method name. Works on for example private or package private methods.- Throws:
Exception
-
expectPrivate
public static <T> org.easymock.IExpectationSetters<T> expectPrivate(Object instance, Object... arguments) throws Exception Used to specify expectations on methods without specifying a method name. Works on for example private or package private methods. PowerMock tries to find a unique method to expect based on the argument parameters. If PowerMock is unable to locate a unique method you need to revert to usingexpectPrivate(Object, String, Object...)
.- Throws:
Exception
-
expectPrivate
public static <T> org.easymock.IExpectationSetters<T> expectPrivate(Object instance, String methodName, Class<?> where, Class<?>[] parameterTypes, Object... arguments) throws Exception Used to specify expectations on methods using the method name at a specific place in the class hierarchy (specified by thewhere
parameter). Works on for example private or package private methods. Use this for overloaded methods.- Throws:
Exception
-
expectPrivate
public static <T> org.easymock.IExpectationSetters<T> expectPrivate(Object instance, String methodName, Class<?> where, Object... arguments) throws Exception Used to specify expectations on methods using the method name at a specific place in the class hierarchy (specified by thewhere
parameter). Works on for example private or package private methods.- Throws:
Exception
-
expectLastCall
This method just delegates to EasyMock class extensionsEasyMock.expectLastCall()
method.- Returns:
- The expectation setter.
- See Also:
-
EasyMock.expectLastCall()
-
niceReplayAndVerify
public static void niceReplayAndVerify()Sometimes it is useful to allow replay and verify on non-mocks. For example when using partial mocking in some tests and no mocking in other test-methods, but using the same setUp and tearDown. -
isEasyMocked
Test if a object is a mock created by EasyMock or not. -
replayAll
Replay all classes and mock objects known by PowerMock. This includes all classes that are prepared for test using thePrepareForTest
orPrepareOnlyThisForTest
annotations and all classes that have had their static initializers removed by using theSuppressStaticInitializationFor
annotation. It also includes all mock instances created by PowerMock such as those created or used bycreateMock(Class, Method...)
,mockStatic(Class, Method...)
,expectNew(Class, Object...)
,createPartialMock(Class, String...)
etc. To make it easy to pass in additional mocks not created by the PowerMock API you can optionally specify them as additionalMocks . These are typically those mock objects you have created using pure EasyMock or EasyMock class extensions. No additional mocks needs to be specified if you're only using PowerMock API methods. Note that the additionalMocks are also automatically verified when invoking theverifyAll()
method.- Parameters:
additionalMocks
- Mocks not created by the PowerMock API. These are typically those mock objects you have created using pure EasyMock or EasyMock class extensions.
-
resetAll
Reset all classes and mock objects known by PowerMock. This includes all classes that are prepared for test using thePrepareForTest
orPrepareOnlyThisForTest
annotations and all classes that have had their static initializers removed by using theSuppressStaticInitializationFor
annotation. It also includes all mock instances created by PowerMock such as those created or used bycreateMock(Class, Method...)
,mockStatic(Class, Method...)
,expectNew(Class, Object...)
,createPartialMock(Class, String...)
etc. To make it easy to pass in additional mocks not created by the PowerMock API you can optionally specify them as additionalMocks . These are typically those mock objects you have created using pure EasyMock or EasyMock class extensions. No additional mocks needs to be specified if you're only using PowerMock API methods.- Parameters:
additionalMocks
- Mocks not created by the PowerMock API. These are typically those mock objects you have created using pure EasyMock or EasyMock class extensions.
-
reset
Reset a list of class mocks. -
reset
Reset a list of mock objects or classes. -
verifyAll
public static void verifyAll()Verify all classes and mock objects known by PowerMock. This includes all classes that are prepared for test using thePrepareForTest
orPrepareOnlyThisForTest
annotations and all classes that have had their static initializers removed by using theSuppressStaticInitializationFor
annotation. It also includes all mock instances created by PowerMock such as those created or used bycreateMock(Class, Method...)
,mockStatic(Class, Method...)
,expectNew(Class, Object...)
,createPartialMock(Class, String...)
etc. Note that all additionalMocks passed to thereplayAll(Object...)
method are also verified here automatically. -
replay
Switches the mocks or classes to replay mode. Note that you must use this method when using PowerMock!- Parameters:
mocks
- mock objects or classes loaded by PowerMock.- Throws:
Exception
- If something unexpected goes wrong.
-
verify
Switches the mocks or classes to verify mode. Note that you must use this method when using PowerMock!- Parameters:
objects
- mock objects or classes loaded by PowerMock.
-
createMockAndExpectNew
Convenience method for createMock followed by expectNew.- Parameters:
type
- The class that should be mocked.arguments
- The constructor arguments.- Returns:
- A mock object of the same type as the mock.
- Throws:
Exception
-
createMockAndExpectNew
public static <T> T createMockAndExpectNew(Class<T> type, Class<?>[] parameterTypes, Object... arguments) throws Exception Convenience method for createMock followed by expectNew when PowerMock cannot determine which constructor to use automatically. This happens when you have one constructor taking a primitive type and another one taking the wrapper type of the primitive. For exampleint
andInteger
.- Parameters:
type
- The class that should be mocked.parameterTypes
- The constructor parameter types.arguments
- The constructor arguments.- Returns:
- A mock object of the same type as the mock.
- Throws:
Exception
-
createNiceMockAndExpectNew
Convenience method for createNiceMock followed by expectNew.- Parameters:
type
- The class that should be mocked.arguments
- The constructor arguments.- Returns:
- A mock object of the same type as the mock.
- Throws:
Exception
-
createNiceMockAndExpectNew
public static <T> T createNiceMockAndExpectNew(Class<T> type, Class<?>[] parameterTypes, Object... arguments) throws Exception Convenience method for createNiceMock followed by expectNew when PowerMock cannot determine which constructor to use automatically. This happens when you have one constructor taking a primitive type and another one taking the wrapper type of the primitive. For exampleint
andInteger
.- Parameters:
type
- The class that should be mocked.parameterTypes
- The constructor parameter types.arguments
- The constructor arguments.- Returns:
- A mock object of the same type as the mock.
- Throws:
Exception
-
createStrictMockAndExpectNew
public static <T> T createStrictMockAndExpectNew(Class<T> type, Object... arguments) throws Exception Convenience method for createStrictMock followed by expectNew.- Parameters:
type
- The class that should be mocked.arguments
- The constructor arguments.- Returns:
- A mock object of the same type as the mock.
- Throws:
Exception
-
createStrictMockAndExpectNew
public static <T> T createStrictMockAndExpectNew(Class<T> type, Class<?>[] parameterTypes, Object... arguments) throws Exception Convenience method for createStrictMock followed by expectNew when PowerMock cannot determine which constructor to use automatically. This happens when you have one constructor taking a primitive type and another one taking the wrapper type of the primitive. For exampleint
andInteger
.- Parameters:
type
- The class that should be mocked.parameterTypes
- The constructor parameter types.arguments
- The constructor arguments.- Returns:
- A mock object of the same type as the mock.
- Throws:
Exception
-
expectNew
public static <T> org.easymock.IExpectationSetters<T> expectNew(Class<T> type, Class<?>[] parameterTypes, Object... arguments) throws Exception Allows specifying expectations on new invocations. For example you might want to throw an exception or return a mock. Note that you must replay the class when using this method since this behavior is part of the class mock. Use this method when you need to specify parameter types for the constructor when PowerMock cannot determine which constructor to use automatically. In most cases you should useexpectNew(Class, Object...)
instead.- Throws:
Exception
-
doExpectNew
private static <T> org.easymock.IExpectationSetters<T> doExpectNew(Class<T> type, MockStrategy mockStrategy, Class<?>[] parameterTypes, Object... arguments) throws Exception - Throws:
Exception
-
expectNew
public static <T> org.easymock.IExpectationSetters<T> expectNew(Class<T> type, Object... arguments) throws Exception Allows specifying expectations on new invocations. For example you might want to throw an exception or return a mock. Note that you must replay the class when using this method since this behavior is part of the class mock.- Throws:
Exception
-
expectNew
public static <T> org.easymock.IExpectationSetters<T> expectNew(String fullyQualifiedName, Object... arguments) throws Exception Allows specifying expectations on new invocations for private member (inner) classes, local or anonymous classes. For example you might want to throw an exception or return a mock. Note that you must replay the class when using this method since this behavior is part of the class mock.- Parameters:
fullyQualifiedName
- The fully-qualified name of the inner/local/anonymous type to expect.arguments
- Optional number of arguments.- Throws:
Exception
-
expectStrictNew
public static <T> org.easymock.IExpectationSetters<T> expectStrictNew(Class<T> type, Object... arguments) throws Exception Allows specifying expectations on new invocations. For example you might want to throw an exception or return a mock. This method checks the order of constructor invocations. Note that you must replay the class when using this method since this behavior is part of the class mock.- Throws:
Exception
-
expectStrictNew
public static <T> org.easymock.IExpectationSetters<T> expectStrictNew(Class<T> type, Class<?>[] parameterTypes, Object... arguments) throws Exception Allows specifying expectations on new invocations. For example you might want to throw an exception or return a mock. Note that you must replay the class when using this method since this behavior is part of the class mock. This method checks the order of constructor invocations. Use this method when you need to specify parameter types for the constructor when PowerMock cannot determine which constructor to use automatically. In most cases you should useexpectNew(Class, Object...)
instead.- Throws:
Exception
-
expectNiceNew
public static <T> org.easymock.IExpectationSetters<T> expectNiceNew(Class<T> type, Object... arguments) throws Exception Allows specifying expectations on new invocations. For example you might want to throw an exception or return a mock. This method allows any number of calls to a new constructor without throwing an exception. Note that you must replay the class when using this method since this behavior is part of the class mock.- Throws:
Exception
-
expectNiceNew
public static <T> org.easymock.IExpectationSetters<T> expectNiceNew(Class<T> type, Class<?>[] parameterTypes, Object... arguments) throws Exception Allows specifying expectations on new invocations. For example you might want to throw an exception or return a mock. Note that you must replay the class when using this method since this behavior is part of the class mock. This method allows any number of calls to a new constructor without throwing an exception. Use this method when you need to specify parameter types for the constructor when PowerMock cannot determine which constructor to use automatically. In most cases you should useexpectNew(Class, Object...)
instead.- Throws:
Exception
-
suppressConstructor
Deprecated.UseMemberModifier.suppress(Constructor[])
instead.Suppress constructor calls on specific constructors only. -
suppressSpecificConstructor
Deprecated.UseMemberModifier.suppress(Constructor)
instead.This method can be used to suppress the code in a specific constructor.- Parameters:
clazz
- The class where the constructor is located.parameterTypes
- The parameter types of the constructor to suppress.
-
suppressConstructor
Deprecated.UseMemberModifier.suppress(Constructor[])
instead.Suppress all constructors in the given class and it's super classes.- Parameters:
classes
- The classes whose constructors will be suppressed.
-
suppressConstructor
Deprecated.UseMemberModifier.suppress(Constructor[])
instead.Suppress all constructors in the given class.- Parameters:
clazz
- The classes whose constructors will be suppressed.excludePrivateConstructors
- optionally keep code in private constructors
-
suppressField
Deprecated.UseMemberModifier.suppress(Field[])
instead.Suppress specific fields. This works on both instance methods and static methods. Note that replay and verify are not needed as this is not part of a mock behavior. -
suppressField
Deprecated.UseMemberModifier.suppress(Field[])
instead.Suppress all fields for these classes. -
suppressField
Deprecated.UseMemberModifier.suppress(Field)
instead.Suppress multiple methods for a class.- Parameters:
clazz
- The class whose methods will be suppressed.fieldNames
- The names of the methods that'll be suppressed. If field names are empty, all fields in the supplied class will be suppressed.
-
suppressMethod
Deprecated.UseMemberModifier.suppress(Method[])
instead.Suppress specific method calls on all types containing this method. This works on both instance methods and static methods. Note that replay and verify are not needed as this is not part of a mock behavior. -
suppressMethod
Deprecated.UseMemberModifier.suppress(Method[])
instead.Suppress all methods for these classes.- Parameters:
cls
- The first class whose methods will be suppressed.additionalClasses
- Additional classes whose methods will be suppressed.
-
suppressMethod
Deprecated.UseMemberModifier.suppress(Method[])
instead.Suppress all methods for these classes.- Parameters:
classes
- Classes whose methods will be suppressed.
-
suppressMethod
public static void suppressMethod(Class<?> clazz, String methodName, String... additionalMethodNames) Deprecated.UseMemberModifier.suppress(Method[])
instead.Suppress multiple methods for a class.- Parameters:
clazz
- The class whose methods will be suppressed.methodName
- The first method to be suppress in classclazz
.additionalMethodNames
- Additional methods to suppress in classclazz
.
-
suppressMethod
Deprecated.UseMemberModifier.suppress(Method[])
instead.Suppress multiple methods for a class.- Parameters:
clazz
- The class whose methods will be suppressed.methodNames
- Methods to suppress in classclazz
.
-
suppressMethod
Deprecated.UseMemberModifier.suppress(Method[])
instead.Suppress all methods for this class.- Parameters:
clazz
- The class which methods will be suppressed.excludePrivateMethods
- optionally not suppress private methods
-
suppressMethod
Deprecated.UseMemberModifier.suppress(Method)
instead.Suppress a specific method call. Use this for overloaded methods. -
doMock
private static <T> T doMock(Class<T> type, boolean isStatic, MockStrategy mockStrategy, org.easymock.ConstructorArgs constructorArgs, Method... methods) -
createReplicaType
-
doCreateMock
-
toSet
-
mergeArgumentTypes
-
doExpectPrivate
private static <T> org.easymock.IExpectationSetters<T> doExpectPrivate(Object instance, Method methodToExpect, Object... arguments) throws Exception - Throws:
Exception
-
replay
-
verifyClass
Note: doesn't clear PowerMock state. -
isNiceReplayAndVerifyMode
private static boolean isNiceReplayAndVerifyMode()
-
MemberModifier.suppress(Constructor[])
instead.