Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.powermock.api.easymock;
import net.sf.cglib.proxy.Enhancer;
import org.easymock.ConstructorArgs;
import org.easymock.IExpectationSetters;
import org.easymock.IMocksControl;
import org.easymock.internal.LastControl;
import org.easymock.internal.MockInvocationHandler;
import org.easymock.internal.MocksControl;
import org.powermock.api.easymock.internal.invocationcontrol.EasyMockMethodInvocationControl;
import org.powermock.api.easymock.internal.invocationcontrol.NewInvocationControlAssertionError;
import org.powermock.api.easymock.internal.invocationcontrol.EasyMockNewInvocationControl;
import org.powermock.api.easymock.internal.mockstrategy.MockStrategy;
import org.powermock.api.easymock.internal.mockstrategy.impl.DefaultMockStrategy;
import org.powermock.api.easymock.internal.mockstrategy.impl.NiceMockStrategy;
import org.powermock.api.easymock.internal.mockstrategy.impl.StrictMockStrategy;
import org.powermock.api.support.SuppressCode;
import org.powermock.api.support.membermodification.MemberModifier;
import org.powermock.core.ClassReplicaCreator;
import org.powermock.core.DefaultFieldValueGenerator;
import org.powermock.core.MockGateway;
import org.powermock.core.MockRepository;
import org.powermock.core.classloader.MockClassLoader;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest;
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
import org.powermock.core.spi.MethodInvocationControl;
import org.powermock.core.spi.NewInvocationControl;
import org.powermock.core.spi.support.InvocationSubstitute;
import org.powermock.reflect.Whitebox;
import org.powermock.reflect.internal.WhiteboxImpl;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
/**
* 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.
*/
public class PowerMock extends MemberModifier {
private static final String NICE_REPLAY_AND_VERIFY_KEY = "PowerMock.niceReplayAndVerify";
static {
MockGateway.MOCK_STANDARD_METHODS = false;
MockGateway.MOCK_GET_CLASS_METHOD = false;
}
/**
* Creates a mock object that supports mocking of final and native methods.
*
* @param the type of the mock object
* @param type the type of the mock object
* @param methods optionally what methods to mock
* @return the mock object.
*/
public static synchronized T createMock(Class type, Method... methods) {
return doMock(type, false, new DefaultMockStrategy(), null, methods);
}
/**
* Creates a mock object that supports mocking of final and native methods.
*
* @param the type of the mock object
* @param type the type of the mock object
* @return the mock object.
*/
public static synchronized T createMock(Class type) {
return doMock(type, false, new DefaultMockStrategy(), null, (Method[]) null);
}
/**
* Creates a mock object that supports mocking of final and native methods
* and invokes a specific constructor.
*
* @param the type of the mock object
* @param type the type of the mock object
* @param constructorArgs The constructor arguments that will be used to invoke a
* special constructor.
* @param methods optionally what methods to mock
* @return the mock object.
*/
public static T createMock(Class type, ConstructorArgs constructorArgs, Method... methods) {
return doMock(type, false, new DefaultMockStrategy(), constructorArgs, methods);
}
/**
* Creates a mock object that supports mocking of final and native methods
* and invokes a specific constructor based on the supplied argument values.
*
* @param the type of the mock object
* @param type the type of the mock object
* @param constructorArguments The constructor arguments that will be used to invoke a
* certain constructor.
* @return the mock object.
*/
public static T createMock(Class type, Object... constructorArguments) {
Constructor> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);
ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);
return doMock(type, false, new DefaultMockStrategy(), constructorArgs, (Method[]) null);
}
/**
* Creates a strict mock object that supports mocking of final and native
* methods.
*
* @param the type of the mock object
* @param type the type of the mock object
* @param methods optionally what methods to mock
* @return the mock object.
*/
public static synchronized T createStrictMock(Class type, Method... methods) {
return doMock(type, false, new StrictMockStrategy(), null, methods);
}
/**
* Creates a strict mock object that supports mocking of final and native
* methods.
*
* @param the type of the mock object
* @param type the type of the mock object
* @return the mock object.
*/
public static synchronized T createStrictMock(Class type) {
return doMock(type, false, new StrictMockStrategy(), null, (Method[]) null);
}
/**
* Creates a nice mock object that supports mocking of final and native
* methods.
*
* @param the type of the mock object
* @param type the type of the mock object
* @param methods optionally what methods to mock
* @return the mock object.
*/
public static synchronized T createNiceMock(Class type, Method... methods) {
return doMock(type, false, new NiceMockStrategy(), null, methods);
}
/**
* Creates a nice mock object that supports mocking of final and native
* methods.
*
* @param the type of the mock object
* @param type the type of the mock object
* @return the mock object.
*/
public static synchronized T createNiceMock(Class type) {
return doMock(type, false, new NiceMockStrategy(), null, (Method[]) null);
}
/**
* Creates a strict mock object that supports mocking of final and native
* methods and invokes a specific constructor.
*
* @param the type of the mock object
* @param type the type of the mock object
* @param constructorArgs The constructor arguments that will be used to invoke a
* special constructor.
* @param methods optionally what methods to mock
* @return the mock object.
*/
public static T createStrictMock(Class type, ConstructorArgs constructorArgs, Method... methods) {
return doMock(type, false, new StrictMockStrategy(), constructorArgs, methods);
}
/**
* Creates a nice mock object that supports mocking of final and native
* methods and invokes a specific constructor.
*
* @param the type of the mock object
* @param type the type of the mock object
* @param constructorArgs The constructor arguments that will be used to invoke a
* special constructor.
* @param methods optionally what methods to mock
* @return the mock object.
*/
public static T createNiceMock(Class type, ConstructorArgs constructorArgs, Method... methods) {
return doMock(type, false, new NiceMockStrategy(), constructorArgs, methods);
}
/**
* Creates a strict mock object that supports mocking of final and native
* methods and invokes a specific constructor based on the supplied argument
* values.
*
* @param the type of the mock object
* @param type the type of the mock object
* @param constructorArguments The constructor arguments that will be used to invoke a
* certain constructor.
* @return the mock object.
*/
public static T createStrictMock(Class type, Object... constructorArguments) {
Constructor> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);
ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);
return doMock(type, false, new StrictMockStrategy(), constructorArgs, (Method[]) null);
}
/**
* Creates a nice mock object that supports mocking of final and native
* methods and invokes a specific constructor based on the supplied argument
* values.
*
* @param the type of the mock object
* @param type the type of the mock object
* @param constructorArguments The constructor arguments that will be used to invoke a
* certain constructor.
* @return the mock object.
*/
public static T createNiceMock(Class type, Object... constructorArguments) {
Constructor> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);
ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);
return doMock(type, false, new NiceMockStrategy(), constructorArgs, (Method[]) null);
}
/**
* Enable static mocking for a class.
*
* @param type the class to enable static mocking
* @param methods optionally what methods to mock
*/
public static synchronized void mockStatic(Class> type, Method... methods) {
doMock(type, true, new DefaultMockStrategy(), null, methods);
}
/**
* Enable static mocking for a class.
*
* @param type the class to enable static mocking
*/
public static synchronized void mockStatic(Class> type) {
doMock(type, true, new DefaultMockStrategy(), null, (Method[]) null);
}
/**
* Enable strict static mocking for a class.
*
* @param type the class to enable static mocking
* @param methods optionally what methods to mock
*/
public static synchronized void mockStaticStrict(Class> type, Method... methods) {
doMock(type, true, new StrictMockStrategy(), null, methods);
}
/**
* Enable strict static mocking for a class.
*
* @param type the class to enable static mocking
*/
public static synchronized void mockStaticStrict(Class> type) {
doMock(type, true, new StrictMockStrategy(), null, (Method[]) null);
}
/**
* Enable nice static mocking for a class.
*
* @param type the class to enable static mocking
* @param methods optionally what methods to mock
*/
public static synchronized void mockStaticNice(Class> type, Method... methods) {
doMock(type, true, new NiceMockStrategy(), null, methods);
}
/**
* Enable nice static mocking for a class.
*
* @param type the class to enable static mocking
*/
public static synchronized void mockStaticNice(Class> type) {
doMock(type, true, new NiceMockStrategy(), null, (Method[]) null);
}
/**
* 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 in {@code type}. 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 the {@link #createMock(Class, Method...)} method
* instead.
*
* @param The type of the mock.
* @param type The type that'll be used to create a mock instance.
* @param methodNames The names of the methods that should be mocked. If
* {@code null}, then this method will have the same effect
* as just calling {@link #createMock(Class, Method...)} with the
* second parameter as {@code new Method[0]} (i.e. all
* methods in that class will be mocked).
* @return A mock object of type .
*/
public static synchronized T createPartialMockForAllMethodsExcept(Class type, String... methodNames) {
if (methodNames != null && methodNames.length == 0) {
return createMock(type);
}
return createMock(type, WhiteboxImpl.getAllMethodExcept(type, 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). Note that you
* cannot uniquely specify a method to exclude using this method if there
* are several methods with the same name in {@code type}. 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 the {@link #createMock(Class, Method...)} method
* instead.
*
* @param The type of the mock.
* @param type The type that'll be used to create a mock instance.
* @param methodNames The names of the methods that should be mocked. If
* {@code null}, then this method will have the same effect
* as just calling {@link #createMock(Class, Method...)} with the
* second parameter as {@code new Method[0]} (i.e. all
* methods in that class will be mocked).
* @return A mock object of type .
*/
public static synchronized T createNicePartialMockForAllMethodsExcept(Class type, String... methodNames) {
if (methodNames != null && methodNames.length == 0) {
return createNiceMock(type);
}
return createNiceMock(type, WhiteboxImpl.getAllMethodExcept(type, 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 in {@code type}. 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 the {@link #createMock(Class, Method...)} method
* instead.
*
* @param The type of the mock.
* @param type The type that'll be used to create a mock instance.
* @param methodNames The names of the methods that should be mocked. If
* {@code null}, then this method will have the same effect
* as just calling {@link #createMock(Class, Method...)} with the
* second parameter as {@code new Method[0]} (i.e. all
* methods in that class will be mocked).
* @return A mock object of type .
*/
public static synchronized T createStrictPartialMockForAllMethodsExcept(Class type, String... methodNames) {
if (methodNames != null && methodNames.length == 0) {
return createStrictMock(type);
}
return createStrictMock(type, WhiteboxImpl.getAllMethodExcept(type, methodNames));
}
/**
* Mock all methods of a class except for a specific one. Use this method
* only if you have several overloaded methods.
*
* @param The type of the mock.
* @param type The type that'll be used to create a mock instance.
* @param methodNameToExclude The name of the method not to mock.
* @param firstArgumentType The type of the first parameter of the method not to mock
* @param moreTypes Optionally more parameter types that defines the method. Note
* that this is only needed to separate overloaded methods.
* @return A mock object of type .
*/
public static synchronized T createPartialMockForAllMethodsExcept(Class type, String methodNameToExclude,
Class> firstArgumentType, Class>... moreTypes) {
/*
* The reason why we've split the first and "additional types" is
* because it should not intervene with the mockAllExcept(type,
* String...methodNames) method.
*/
final Class>[] argumentTypes = mergeArgumentTypes(firstArgumentType, moreTypes);
return createMock(type, WhiteboxImpl.getAllMethodsExcept(type, methodNameToExclude, argumentTypes));
}
/**
* Mock all methods of a class except for a specific one nicely. Use this
* method only if you have several overloaded methods.
*
* @param The type of the mock.
* @param type The type that'll be used to create a mock instance.
* @param methodNameToExclude The name of the method not to mock.
* @param firstArgumentType The type of the first parameter of the method not to mock
* @param moreTypes Optionally more parameter types that defines the method. Note
* that this is only needed to separate overloaded methods.
* @return A mock object of type .
*/
public static synchronized T createNicePartialMockForAllMethodsExcept(Class type,
String methodNameToExclude, Class> firstArgumentType, Class>... moreTypes) {
/*
* The reason why we've split the first and "additional types" is
* because it should not intervene with the mockAllExcept(type,
* String...methodNames) method.
*/
final Class>[] argumentTypes = mergeArgumentTypes(firstArgumentType, moreTypes);
return createNiceMock(type, WhiteboxImpl.getAllMethodsExcept(type, methodNameToExclude, argumentTypes));
}
/**
* Mock all methods of a class except for a specific one strictly. Use this
* method only if you have several overloaded methods.
*
* @param The type of the mock.
* @param type The type that'll be used to create a mock instance.
* @param methodNameToExclude The name of the method not to mock.
* @param firstArgumentType The type of the first parameter of the method not to mock
* @param moreTypes Optionally more parameter types that defines the method. Note
* that this is only needed to separate overloaded methods.
* @return A mock object of type .
*/
public static synchronized T createStrictPartialMockForAllMethodsExcept(Class type,
String methodNameToExclude, Class> firstArgumentType, Class>... moreTypes) {
/*
* The reason why we've split the first and "additional types" is
* because it should not intervene with the mockAllExcept(type,
* String...methodNames) method.
*/
final Class>[] argumentTypes = mergeArgumentTypes(firstArgumentType, moreTypes);
return createStrictMock(type, WhiteboxImpl.getAllMethodsExcept(type, methodNameToExclude, argumentTypes));
}
/**
* Mock a single specific method. Use this to handle overloaded methods.
*
* @param The type of the mock.
* @param type The type that'll be used to create a mock instance.
* @param methodNameToMock The name of the method to mock
* @param firstArgumentType The type of the first parameter of the method to mock
* @param additionalArgumentTypes Optionally more parameter types that defines the method. Note
* that this is only needed to separate overloaded methods.
* @return A mock object of type .
*/
public static synchronized T createPartialMock(Class type, String methodNameToMock,
Class> firstArgumentType, Class>... additionalArgumentTypes) {
return doMockSpecific(type, new DefaultMockStrategy(), new String[]{methodNameToMock}, null,
mergeArgumentTypes(firstArgumentType, additionalArgumentTypes));
}
/**
* Strictly mock a single specific method. Use this to handle overloaded
* methods.
*
* @param The type of the mock.
* @param type The type that'll be used to create a mock instance.
* @param methodNameToMock The name of the method to mock
* @param firstArgumentType The type of the first parameter of the method to mock
* @param additionalArgumentTypes Optionally more parameter types that defines the method. Note
* that this is only needed to separate overloaded methods.
* @return A mock object of type .
*/
public static synchronized T createStrictPartialMock(Class type, String methodNameToMock,
Class> firstArgumentType, Class>... additionalArgumentTypes) {
return doMockSpecific(type, new StrictMockStrategy(), new String[]{methodNameToMock}, null,
mergeArgumentTypes(firstArgumentType, additionalArgumentTypes));
}
/**
* Nicely mock a single specific method. Use this to handle overloaded
* methods.
*
* @param The type of the mock.
* @param type The type that'll be used to create a mock instance.
* @param methodNameToMock The name of the method to mock
* @param firstArgumentType The type of the first parameter of the method to mock
* @param additionalArgumentTypes Optionally more parameter types that defines the method. Note
* that this is only needed to separate overloaded methods.
* @return A mock object of type .
*/
public static synchronized T createNicePartialMock(Class type, String methodNameToMock,
Class> firstArgumentType, Class>... additionalArgumentTypes) {
return doMockSpecific(type, new NiceMockStrategy(), new String[]{methodNameToMock}, null,
mergeArgumentTypes(firstArgumentType, additionalArgumentTypes));
}
/**
* Mock a single static method.
*
* @param clazz The class where the method is specified in.
* @param methodNameToMock The first argument
* @param firstArgumentType The first argument type.
* @param additionalArgumentTypes Optional additional argument types.
*/
public static synchronized void mockStaticPartial(Class> clazz, String methodNameToMock,
Class> firstArgumentType, Class>... additionalArgumentTypes) {
doMockSpecific(clazz, new DefaultMockStrategy(), new String[]{methodNameToMock}, null,
mergeArgumentTypes(firstArgumentType, additionalArgumentTypes));
}
/**
* Mock a single static method (strict).
*
* @param clazz The class where the method is specified in.
* @param methodNameToMock The first argument
* @param firstArgumentType The first argument type.
* @param additionalArgumentTypes Optional additional argument types.
*/
public static synchronized void mockStaticPartialStrict(Class> clazz, String methodNameToMock,
Class> firstArgumentType, Class>... additionalArgumentTypes) {
doMockSpecific(clazz, new StrictMockStrategy(), new String[]{methodNameToMock}, null,
mergeArgumentTypes(firstArgumentType, additionalArgumentTypes));
}
/**
* Mock a single static method (nice).
*
* @param clazz The class where the method is specified in.
* @param methodNameToMock The first argument
* @param firstArgumentType The first argument type.
* @param additionalArgumentTypes Optional additional argument types.
*/
public static synchronized void mockStaticPartialNice(Class> clazz, String methodNameToMock,
Class> firstArgumentType, Class>... additionalArgumentTypes) {
doMockSpecific(clazz, new NiceMockStrategy(), new String[]{methodNameToMock}, null,
mergeArgumentTypes(firstArgumentType, additionalArgumentTypes));
}
/**
* 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 in
* {@code type}. 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 the
* {@link #mockStatic(Class, Method...)} method instead.
*
* @param clazz The class that contains the static methods that should be
* mocked.
* @param methodNames The names of the methods that should be mocked. If
* {@code null}, then this method will have the same effect
* as just calling {@link #mockStatic(Class, Method...)} with the
* second parameter as {@code new Method[0]} (i.e. all
* methods in that class will be mocked).
*/
public static synchronized void mockStaticPartial(Class> clazz, String... methodNames) {
mockStatic(clazz, Whitebox.getMethods(clazz, 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). Note that you cannot uniquely specify a method
* to mock using this method if there are several methods with the same name
* in {@code type}. 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 the
* {@link #mockStaticStrict(Class, Method...)} method instead.
*
* @param clazz The class that contains the static methods that should be
* mocked.
* @param methodNames The names of the methods that should be mocked. If
* {@code null}, then this method will have the same effect
* as just calling {@link #mockStatic(Class, Method...)} with the
* second parameter as {@code new Method[0]} (i.e. all
* methods in that class will be mocked).
*/
public static synchronized void mockStaticPartialStrict(Class> clazz, String... methodNames) {
mockStaticStrict(clazz, Whitebox.getMethods(clazz, 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). Note that you cannot uniquely specify a method to mock
* using this method if there are several methods with the same name in
* {@code type}. 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 the
* {@link #mockStaticStrict(Class, Method...)} method instead.
*
* @param clazz The class that contains the static methods that should be
* mocked.
* @param methodNames The names of the methods that should be mocked. If
* {@code null}, then this method will have the same effect
* as just calling {@link #mockStatic(Class, Method...)} with the
* second parameter as {@code new Method[0]} (i.e. all
* methods in that class will be mocked).
*/
public static synchronized void mockStaticPartialNice(Class> clazz, String... methodNames) {
mockStaticNice(clazz, Whitebox.getMethods(clazz, methodNames));
}
static T doMockSpecific(Class type, MockStrategy mockStrategy, String[] methodNamesToMock,
ConstructorArgs constructorArgs, Class>... argumentTypes) {
List methods = new LinkedList();
for (String methodName : methodNamesToMock) {
methods.add(WhiteboxImpl.findMethodOrThrowException(type, methodName, argumentTypes));
}
final Method[] methodArray = methods.toArray(new Method[0]);
if (WhiteboxImpl.areAllMethodsStatic(methodArray)) {
if (mockStrategy instanceof DefaultMockStrategy) {
mockStatic(type, methodArray);
} else if (mockStrategy instanceof StrictMockStrategy) {
mockStaticStrict(type, methodArray);
} else {
mockStaticNice(type, methodArray);
}
return null;
}
T mock = null;
if (mockStrategy instanceof DefaultMockStrategy) {
mock = createMock(type, constructorArgs, methodArray);
} else if (mockStrategy instanceof StrictMockStrategy) {
mock = createStrictMock(type, constructorArgs, methodArray);
} else {
mock = createNiceMock(type, constructorArgs, methodArray);
}
return mock;
}
/**
* 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 in {@code type}.
* 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 the {@link #createMock(Class, Method...)} method
* instead.
*
* @param The type of the mock.
* @param type The type that'll be used to create a mock instance.
* @param methodNames The names of the methods that should be mocked. If
* {@code null}, then this method will have the same effect
* as just calling {@link #createMock(Class, Method...)} with the
* second parameter as {@code new Method[0]} (i.e. all
* methods in that class will be mocked).
* @return A mock object of type .
*/
public static synchronized T createPartialMock(Class type, String... methodNames) {
return createMock(type, Whitebox.getMethods(type, 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).
* Note that you cannot uniquely specify a method to mock using this method
* if there are several methods with the same name in {@code type}.
* 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 the {@link #createMock(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");
*
*
* @param The type of the mock.
* @param type The type that'll be used to create a mock instance.
* @param where Where in the class hierarchy the methods resides.
* @param methodNames The names of the methods that should be mocked. If
* {@code null}, then this method will have the same effect
* as just calling {@link #createMock(Class, Method...)} with the
* second parameter as {@code new Method[0]} (i.e. all
* methods in that class will be mocked).
* @return A mock object of type .
*/
public static synchronized T createPartialMock(Class type, Class super T> where, String... methodNames) {
return createMock(type, Whitebox.getMethods(where, 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 in
* {@code type}. 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 the
* {@link #createMock(Class, Method...)} method instead.
*
* @param The type of the mock.
* @param type The type that'll be used to create a mock instance.
* @param methodNames The names of the methods that should be mocked. If
* {@code null}, then this method will have the same effect
* as just calling {@link #createMock(Class, Method...)} with the
* second parameter as {@code new Method[0]} (i.e. all
* methods in that class will be mocked).
* @return A mock object of type .
*/
public static synchronized T createStrictPartialMock(Class type, String... methodNames) {
return createStrictMock(type, Whitebox.getMethods(type, 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 in
* {@code type}. 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 the
* {@link #createMock(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");
*
*
* @param The type of the mock.
* @param type The type that'll be used to create a mock instance.
* @param where Where in the class hierarchy the methods resides.
* @param methodNames The names of the methods that should be mocked. If
* {@code null}, then this method will have the same effect
* as just calling {@link #createMock(Class, Method...)} with the
* second parameter as {@code new Method[0]} (i.e. all
* methods in that class will be mocked).
* @return A mock object of type .
*/
public static synchronized T createStrictPartialMock(Class type, Class super T> where,
String... methodNames) {
return createStrictMock(type, Whitebox.getMethods(where, 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 in
* {@code type}. 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 the
* {@link #createMock(Class, Method...)} method instead.
*
* @param The type of the mock.
* @param type The type that'll be used to create a mock instance.
* @param methodNames The names of the methods that should be mocked. If
* {@code null}, then this method will have the same effect
* as just calling {@link #createMock(Class, Method...)} with the
* second parameter as {@code new Method[0]} (i.e. all
* methods in that class will be mocked).
* @return A mock object of type .
*/
public static synchronized T createNicePartialMock(Class type, String... methodNames) {
return createNiceMock(type, Whitebox.getMethods(type, 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 in
* {@code type}. 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 the
* {@link #createMock(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");
*
*
* @param The type of the mock.
* @param type The type that'll be used to create a mock instance.
* @param where Where in the class hierarchy the methods resides.
* @param methodNames The names of the methods that should be mocked. If
* {@code null}, then this method will have the same effect
* as just calling {@link #createMock(Class, Method...)} with the
* second parameter as {@code new Method[0]} (i.e. all
* methods in that class will be mocked).
* @return A mock object of type .
*/
public static synchronized T createNicePartialMock(Class type, Class super T> where, String... methodNames) {
return createNiceMock(type, Whitebox.getMethods(where, 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). The
* mock object created will support mocking of final methods and invokes the
* default constructor (even if it's private).
*
* @param the type of the mock object
* @param type the type of the mock object
* @param methodNames The names of the methods that should be mocked. If
* {@code null}, then this method will have the same effect
* as just calling {@link #createMock(Class, Method...)} with the
* second parameter as {@code new Method[0]} (i.e. all
* methods in that class will be mocked).
* @return the mock object.
*/
public static T createPartialMockAndInvokeDefaultConstructor(Class type, String... methodNames)
throws Exception {
return createMock(type, new ConstructorArgs(Whitebox.getConstructor(type)),
Whitebox.getMethods(type, 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). The mock object created will support mocking of final methods and
* invokes the default constructor (even if it's private).
*
* @param the type of the mock object
* @param type the type of the mock object
* @param methodNames The names of the methods that should be mocked. If
* {@code null}, then this method will have the same effect
* as just calling {@link #createMock(Class, Method...)} with the
* second parameter as {@code new Method[0]} (i.e. all
* methods in that class will be mocked).
* @return the mock object.
*/
public static T createNicePartialMockAndInvokeDefaultConstructor(Class type, String... methodNames)
throws Exception {
return createNiceMock(type, new ConstructorArgs(Whitebox.getConstructor(type)),
Whitebox.getMethods(type, 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). The mock object created will support mocking of final methods and
* invokes the default constructor (even if it's private).
*
* @param the type of the mock object
* @param type the type of the mock object
* @param methodNames The names of the methods that should be mocked. If
* {@code null}, then this method will have the same effect
* as just calling {@link #createMock(Class, Method...)} with the
* second parameter as {@code new Method[0]} (i.e. all
* methods in that class will be mocked).
* @return the mock object.
*/
public static T createStrictPartialMockAndInvokeDefaultConstructor(Class type, String... methodNames)
throws Exception {
return createStrictMock(type, new ConstructorArgs(Whitebox.getConstructor(type)),
Whitebox.getMethods(type, 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). The
* mock object created will support mocking of final and native methods and
* invokes a specific constructor based on the supplied argument values.
*
* @param the type of the mock object
* @param type the type of the mock object
* @param methodNames The names of the methods that should be mocked. If
* {@code null}, then this method will have the same effect
* as just calling {@link #createMock(Class, Method...)} with the
* second parameter as {@code new Method[0]} (i.e. all
* methods in that class will be mocked).
* @param constructorArguments The constructor arguments that will be used to invoke a
* certain constructor. (optional)
* @return the mock object.
*/
public static T createPartialMock(Class type, String[] methodNames, Object... constructorArguments) {
Constructor> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);
ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);
return doMock(type, false, new DefaultMockStrategy(), constructorArgs, Whitebox.getMethods(type, 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). The mock object created will support mocking of final and
* native methods and invokes a specific constructor based on the supplied
* argument values.
*
* @param the type of the mock object
* @param type the type of the mock object
* @param methodNames The names of the methods that should be mocked. If
* {@code null}, then this method will have the same effect
* as just calling {@link #createMock(Class, Method...)} with the
* second parameter as {@code new Method[0]} (i.e. all
* methods in that class will be mocked).
* @param constructorArguments The constructor arguments that will be used to invoke a
* certain constructor. (optional)
* @return the mock object.
*/
public static T createStrictPartialMock(Class type, String[] methodNames, Object... constructorArguments) {
Constructor> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);
ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);
return doMock(type, false, new StrictMockStrategy(), constructorArgs, Whitebox.getMethods(type, 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). The mock object created will support mocking of final and native
* methods and invokes a specific constructor based on the supplied argument
* values.
*
* @param the type of the mock object
* @param type the type of the mock object
* @param methodNames The names of the methods that should be mocked. If
* {@code null}, then this method will have the same effect
* as just calling {@link #createMock(Class, Method...)} with the
* second parameter as {@code new Method[0]} (i.e. all
* methods in that class will be mocked).
* @param constructorArguments The constructor arguments that will be used to invoke a
* certain constructor. (optional)
* @return the mock object.
*/
public static T createNicePartialMock(Class type, String[] methodNames, Object... constructorArguments) {
Constructor> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);
ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);
return doMock(type, false, new NiceMockStrategy(), constructorArgs, Whitebox.getMethods(type, 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). 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.
*
* @param the type of the mock object
* @param type the type of the mock object
* @param methodName The names of the methods that should be mocked. If
* {@code null}, then this method will have the same effect
* as just calling {@link #createMock(Class, Method...)} with the
* second parameter as {@code new Method[0]} (i.e. all
* methods in that class will be mocked).
* @param methodParameterTypes Parameter types that defines the method. Note that this is
* only needed to separate overloaded methods.
* @param constructorArguments The constructor arguments that will be used to invoke a
* certain constructor. (optional)
* @return the mock object.
*/
public static T createPartialMock(Class type, String methodName, Class>[] methodParameterTypes,
Object... constructorArguments) {
Constructor> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);
ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);
return doMockSpecific(type, new DefaultMockStrategy(), new String[]{methodName}, constructorArgs,
methodParameterTypes);
}
/**
* 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.
*
* @param the type of the mock object
* @param type the type of the mock object
* @param methodName The names of the methods that should be mocked. If
* {@code null}, then this method will have the same effect
* as just calling {@link #createMock(Class, Method...)} with the
* second parameter as {@code new Method[0]} (i.e. all
* methods in that class will be mocked).
* @param methodParameterTypes Parameter types that defines the method. Note that this is
* only needed to separate overloaded methods.
* @param constructorArguments The constructor arguments that will be used to invoke a
* certain constructor. (optional)
* @return the mock object.
*/
public static T createStrictPartialMock(Class type, String methodName, Class>[] methodParameterTypes,
Object... constructorArguments) {
Constructor> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);
ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);
return doMockSpecific(type, new StrictMockStrategy(), new String[]{methodName}, constructorArgs,
methodParameterTypes);
}
/**
* 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.
*
* @param the type of the mock object
* @param type the type of the mock object
* @param methodName The names of the methods that should be mocked. If
* {@code null}, then this method will have the same effect
* as just calling {@link #createMock(Class, Method...)} with the
* second parameter as {@code new Method[0]} (i.e. all
* methods in that class will be mocked).
* @param methodParameterTypes Parameter types that defines the method. Note that this is
* only needed to separate overloaded methods.
* @param constructorArguments The constructor arguments that will be used to invoke a
* certain constructor. (optional)
* @return the mock object.
*/
public static T createNicePartialMock(Class type, String methodName, Class>[] methodParameterTypes,
Object... constructorArguments) {
Constructor> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);
ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);
return doMockSpecific(type, new NiceMockStrategy(), new String[]{methodName}, constructorArgs,
methodParameterTypes);
}
/**
* 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.
*
* @param the type of the mock object
* @param type the type of the mock object
* @param methodName The names of the methods that should be mocked. If
* {@code null}, then this method will have the same effect
* as just calling {@link #createMock(Class, Method...)} with the
* second parameter as {@code new Method[0]} (i.e. all
* methods in that class will be mocked).
* @param methodParameterTypes Parameter types that defines the method. Note that this is
* only needed to separate overloaded methods.
* @param constructorArguments The constructor arguments that will be used to invoke a
* certain constructor.
* @param constructorParameterTypes Parameter types that defines the constructor that should be
* invoked. Note that this is only needed to separate overloaded
* constructors.
* @return the mock object.
*/
public static T createPartialMock(Class type, String methodName, Class>[] methodParameterTypes,
Object[] constructorArguments, Class>[] constructorParameterTypes) {
ConstructorArgs constructorArgs = new ConstructorArgs(Whitebox.getConstructor(type, constructorParameterTypes),
constructorArguments);
return doMockSpecific(type, new DefaultMockStrategy(), new String[]{methodName}, constructorArgs,
methodParameterTypes);
}
/**
* 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.
*
* @param the type of the mock object
* @param type the type of the mock object
* @param methodName The names of the methods that should be mocked. If
* {@code null}, then this method will have the same effect
* as just calling {@link #createMock(Class, Method...)} with the
* second parameter as {@code new Method[0]} (i.e. all
* methods in that class will be mocked).
* @param methodParameterTypes Parameter types that defines the method. Note that this is
* only needed to separate overloaded methods.
* @param constructorArguments The constructor arguments that will be used to invoke a
* certain constructor.
* @param constructorParameterTypes Parameter types that defines the constructor that should be
* invoked. Note that this is only needed to separate overloaded
* constructors.
* @return the mock object.
*/
public static T createStrictPartialMock(Class type, String methodName, Class>[] methodParameterTypes,
Object[] constructorArguments, Class>[] constructorParameterTypes) {
ConstructorArgs constructorArgs = new ConstructorArgs(Whitebox.getConstructor(type, constructorParameterTypes),
constructorArguments);
return doMockSpecific(type, new StrictMockStrategy(), new String[]{methodName}, constructorArgs,
methodParameterTypes);
}
/**
* 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.
*
* @param the type of the mock object
* @param type the type of the mock object
* @param methodName The names of the methods that should be mocked. If
* {@code null}, then this method will have the same effect
* as just calling {@link #createMock(Class, Method...)} with the
* second parameter as {@code new Method[0]} (i.e. all
* methods in that class will be mocked).
* @param methodParameterTypes Parameter types that defines the method. Note that this is
* only needed to separate overloaded methods.
* @param constructorArguments The constructor arguments that will be used to invoke a
* certain constructor.
* @param constructorParameterTypes Parameter types that defines the constructor that should be
* invoked. Note that this is only needed to separate overloaded
* constructors.
* @return the mock object.
*/
public static T createNicePartialMock(Class type, String methodName, Class>[] methodParameterTypes,
Object[] constructorArguments, Class>[] constructorParameterTypes) {
ConstructorArgs constructorArgs = new ConstructorArgs(Whitebox.getConstructor(type, constructorParameterTypes),
constructorArguments);
return doMockSpecific(type, new NiceMockStrategy(), new String[]{methodName}, constructorArgs,
methodParameterTypes);
}
/**
* Used to specify expectations on private static methods. If possible use
* variant with only method name.
*/
public static synchronized IExpectationSetters expectPrivate(Class> clazz, Method method,
Object... arguments) throws Exception {
return doExpectPrivate(clazz, method, arguments);
}
/**
* Used to specify expectations on private methods. If possible use variant
* with only method name.
*/
public static synchronized IExpectationSetters expectPrivate(Object instance, Method method,
Object... arguments) throws Exception {
return doExpectPrivate(instance, method, arguments);
}
/**
* Used to specify expectations on private methods. Use this method to
* handle overloaded methods.
*/
@SuppressWarnings("all")
public static synchronized IExpectationSetters expectPrivate(Object instance, String methodName,
Class>[] parameterTypes, Object... arguments) throws Exception {
if (arguments == null) {
arguments = new Object[0];
}
if (instance == null) {
throw new IllegalArgumentException("instance cannot be null.");
} else if (arguments.length != parameterTypes.length) {
throw new IllegalArgumentException(
"The length of the arguments must be equal to the number of parameter types.");
}
Method foundMethod = Whitebox.getMethod(instance.getClass(), methodName, parameterTypes);
WhiteboxImpl.throwExceptionIfMethodWasNotFound(instance.getClass(), methodName, foundMethod, parameterTypes);
return doExpectPrivate(instance, foundMethod, arguments);
}
/**
* Used to specify expectations on methods using the method name. Works on
* for example private or package private methods.
*/
public static synchronized IExpectationSetters expectPrivate(Object instance, String methodName,
Object... arguments) throws Exception {
if (instance == null) {
throw new IllegalArgumentException("Instance or class cannot be null.");
}
return expectPrivate(instance, methodName, Whitebox.getType(instance), arguments);
}
/**
* 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 using
* {@link #expectPrivate(Object, String, Object...)}.
*/
public static synchronized IExpectationSetters expectPrivate(Object instance, Object... arguments)
throws Exception {
return expectPrivate(instance, null, Whitebox.getType(instance), arguments);
}
/**
* Used to specify expectations on methods using the method name at a
* specific place in the class hierarchy (specified by the
* {@code where} parameter). Works on for example private or package
* private methods.
*
* Use this for overloaded methods.
*/
public static synchronized IExpectationSetters expectPrivate(Object instance, String methodName,
Class> where, Class>[] parameterTypes, Object... arguments) throws Exception {
if (instance == null) {
throw new IllegalArgumentException("Instance or class to expect cannot be null.");
}
Method[] methods = null;
if (methodName != null) {
if (parameterTypes == null) {
methods = Whitebox.getMethods(where, methodName);
} else {
methods = new Method[]{Whitebox.getMethod(where, methodName, parameterTypes)};
}
}
Method methodToExpect;
if (methods != null && methods.length == 1) {
methodToExpect = methods[0];
} else {
methodToExpect = WhiteboxImpl.findMethodOrThrowException(instance, null, methodName, arguments);
}
return doExpectPrivate(instance, methodToExpect, arguments);
}
/**
* Used to specify expectations on methods using the method name at a
* specific place in the class hierarchy (specified by the
* {@code where} parameter). Works on for example private or package
* private methods.
*/
public static synchronized IExpectationSetters expectPrivate(Object instance, String methodName,
Class> where, Object... arguments) throws Exception {
return expectPrivate(instance, methodName, where, null, arguments);
}
/**
* This method just delegates to EasyMock class extensions
* {@link org.easymock.EasyMock#expectLastCall()} method.
*
* @return The expectation setter.
* @see org.easymock.EasyMock#expectLastCall()
*/
public static synchronized IExpectationSetters