/** * Returns a constructor specified in declaringClass. * * @param declaringClass * The declaringClass of the class where the constructor is * located. * @param parameterTypes * All parameter types of the constructor (may be * {@code null}). * @return A {@code java.lang.reflect.Constructor}. * @throws ConstructorNotFoundException * if the constructor cannot be found. */ @SuppressWarnings("unchecked") public static <T> Constructor<T> constructor(Class<T> declaringClass, Class<?>... parameterTypes) { return (Constructor<T>) WhiteboxImpl.findUniqueConstructorOrThrowException(declaringClass, (Object[]) parameterTypes); }
/** * Returns a constructor specified in declaringClass. * * @param declaringClass * The declaringClass of the class where the constructor is * located. * @param parameterTypes * All parameter types of the constructor (may be * <code>null</code>). * @return A <code>java.lang.reflect.Constructor</code>. * @throws ConstructorNotFoundException * if the constructor cannot be found. */ @SuppressWarnings("unchecked") public static <T> Constructor<T> constructor(Class<T> declaringClass, Class<?>... parameterTypes) { return (Constructor<T>) WhiteboxImpl.findUniqueConstructorOrThrowException(declaringClass, (Object[]) parameterTypes); }
@SuppressWarnings({"unchecked", "rawtypes"}) private OngoingStubbing<T> createNewSubstituteMock(Class<T> type, Class<?>[] parameterTypes, Object... arguments) throws Exception { if (type == null) { throw new IllegalArgumentException("type cannot be null"); } final Class<T> unmockedType = (Class<T>) WhiteboxImpl.getOriginalUnmockedType(type); if (parameterTypes == null) { WhiteboxImpl.findUniqueConstructorOrThrowException(type, arguments); } else { WhiteboxImpl.getConstructor(unmockedType, parameterTypes); } NewInvocationControl<OngoingStubbing<T>> newInvocationControl = createNewInvocationControl(type, unmockedType); return newInvocationControl.expectSubstitutionLogic(arguments); }
/** * 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 <T> * 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> T createNiceMock(Class<T> type, Object... constructorArguments) { Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments); ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments); return doMock(type, false, new NiceMockStrategy(), constructorArgs, (Method[]) null); }
/** * Creates a mock object that supports mocking of final and native methods * and invokes a specific constructor based on the supplied argument values. * * @param <T> * 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> T createMock(Class<T> 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 and invokes a specific constructor based on the supplied argument * values. * * @param <T> * 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> T createStrictMock(Class<T> type, Object... constructorArguments) { Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments); ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments); return doMock(type, false, new StrictMockStrategy(), constructorArgs, (Method[]) null); }
/** * 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 <T> * 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</code>, then this method will have the same effect * as just calling {@link #createMock(Class, Method...)} with the * second parameter as <code>new Method[0]</code> (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> T createStrictPartialMock(Class<T> 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 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 <T> * 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</code>, then this method will have the same effect * as just calling {@link #createMock(Class, Method...)} with the * second parameter as <code>new Method[0]</code> (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> T createPartialMock(Class<T> 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 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 <T> * 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</code>, then this method will have the same effect * as just calling {@link #createMock(Class, Method...)} with the * second parameter as <code>new Method[0]</code> (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> T createNicePartialMock(Class<T> 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 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 <T> * 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</code>, then this method will have the same effect * as just calling {@link #createMock(Class, Method...)} with the * second parameter as <code>new Method[0]</code> (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> T createStrictPartialMock(Class<T> 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 <T> * 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</code>, then this method will have the same effect * as just calling {@link #createMock(Class, Method...)} with the * second parameter as <code>new Method[0]</code> (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> T createNicePartialMock(Class<T> 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). The * mock object created will support mocking of final and native methods and * invokes a specific constructor based on the supplied argument values. * * @param <T> * 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</code>, then this method will have the same effect * as just calling {@link #createMock(Class, Method...)} with the * second parameter as <code>new Method[0]</code> (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> T createPartialMock(Class<T> 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)); }
if (!isNiceMock) { if (parameterTypes == null) { WhiteboxImpl.findUniqueConstructorOrThrowException(type, arguments); } else { WhiteboxImpl.getConstructor(unmockedType, parameterTypes);
@SuppressWarnings({"unchecked", "rawtypes"}) private <T> OngoingStubbing<T> createNewSubstituteMock(Class<T> type, Class<?>[] parameterTypes, Object... arguments) throws Exception { if (type == null) { throw new IllegalArgumentException("type cannot be null"); } final Class<T> unmockedType = (Class<T>) WhiteboxImpl.getOriginalUnmockedType(type); if (parameterTypes == null) { WhiteboxImpl.findUniqueConstructorOrThrowException(type, arguments); } else { WhiteboxImpl.getConstructor(unmockedType, parameterTypes); } /* * Check if this type has been mocked before */ NewInvocationControl<OngoingStubbing<T>> newInvocationControl = (NewInvocationControl<OngoingStubbing<T>>) MockRepository.getNewInstanceControl(unmockedType); if (newInvocationControl == null) { InvocationSubstitute<T> mock = getMockCreator().createMock(InvocationSubstitute.class, false, false, null, null, (Method[]) null); newInvocationControl = createNewInvocationControl(mock); MockRepository.putNewInstanceControl(type, newInvocationControl); MockRepository.addObjectsToAutomaticallyReplayAndVerify(WhiteboxImpl.getOriginalUnmockedType(type)); } return newInvocationControl.expectSubstitutionLogic(arguments); }