/** * Do invoke method. * * @param <T> the generic type * @param tested the tested * @param declaringClass the declaring class * @param methodToExecute the method to execute * @param arguments the arguments * @return the t * @throws Exception the exception */ @SuppressWarnings("unchecked") private static <T> T doInvokeMethod(Object tested, Class<?> declaringClass, String methodToExecute, Object... arguments) throws Exception { Method methodToInvoke = findMethodOrThrowException(tested, declaringClass, methodToExecute, arguments); // Invoke test return (T) performMethodInvocation(tested, methodToInvoke, arguments); }
/** * Do invoke method. * * @param <T> * the generic type * @param tested * the tested * @param declaringClass * the declaring class * @param methodToExecute * the method to execute * @param arguments * the arguments * @return the t * @throws Exception * the exception */ @SuppressWarnings("unchecked") private static <T> T doInvokeMethod(Object tested, Class<?> declaringClass, String methodToExecute, Object... arguments) throws Exception { Method methodToInvoke = findMethodOrThrowException(tested, declaringClass, methodToExecute, arguments); // Invoke test return (T) performMethodInvocation(tested, methodToInvoke, arguments); }
/** * Suppress a specific method call. Use this for overloaded methods. */ public static synchronized void suppressMethod(Class<?> clazz, String methodName, Class<?>[] parameterTypes) { Method method = null; if (parameterTypes.length > 0) { method = Whitebox.getMethod(clazz, methodName, parameterTypes); } else { method = WhiteboxImpl.findMethodOrThrowException(clazz, methodName, parameterTypes); } MockRepository.addMethodToSuppress(method); }
/** * Suppress a specific method call. Use this for overloaded methods. */ public static synchronized void suppressMethod(Class<?> clazz, String methodName, Class<?>[] parameterTypes) { Method method = null; if (parameterTypes.length > 0) { method = Whitebox.getMethod(clazz, methodName, parameterTypes); } else { method = WhiteboxImpl.findMethodOrThrowException(clazz, methodName, parameterTypes); } MockRepository.addMethodToSuppress(method); }
/** * Used to specify expectations on methods using the method name at a * specific place in the class hierarchy (specified by the * <code>where</code> parameter). Works on for example private or package * private methods. * <p> * Use this for overloaded methods. */ public static synchronized <T> IExpectationSetters<T> 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); }
static <T> T doMockSpecific(Class<T> type, MockStrategy mockStrategy, String[] methodNamesToMock, ConstructorArgs constructorArgs, Class<?>... argumentTypes) { List<Method> methods = new LinkedList<Method>(); 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; }