/** * Add a method that should be intercepted and return another value ( * {@code returnObject}) (i.e. the method is stubbed). */ public static void stubMethod(Method method, Object returnObject) { MockRepository.putMethodToStub(method, returnObject); }
/** * Add a method that should be intercepted and return another value ( * <code>returnObject</code>) (i.e. the method is stubbed). */ public static void stubMethod(Method method, Object returnObject) { MockRepository.putMethodToStub(method, returnObject); }
/** * Add a method that should be intercepted and return another value ( * {@code returnObject}) (i.e. the method is stubbed). */ public static void stubMethod(Class<?> declaringClass, String methodName, Object returnObject) { if (declaringClass == null) { throw new IllegalArgumentException("declaringClass cannot be null"); } if (methodName == null || methodName.length() == 0) { throw new IllegalArgumentException("methodName cannot be empty"); } Method[] methods = Whitebox.getMethods(declaringClass, methodName); if (methods.length == 0) { throw new MethodNotFoundException(String.format("Couldn't find a method with name %s in the class hierarchy of %s", methodName, declaringClass.getName())); } else if (methods.length > 1) { throw new TooManyMethodsFoundException(String.format("Found %d methods with name %s in the class hierarchy of %s.", methods.length, methodName, declaringClass.getName())); } MockRepository.putMethodToStub(methods[0], returnObject); }
/** * Add a method that should be intercepted and return another value ( * <code>returnObject</code>) (i.e. the method is stubbed). */ public static void stubMethod(Class<?> declaringClass, String methodName, Object returnObject) { if (declaringClass == null) { throw new IllegalArgumentException("declaringClass cannot be null"); } if (methodName == null || methodName.length() == 0) { throw new IllegalArgumentException("methodName cannot be empty"); } Method[] methods = Whitebox.getMethods(declaringClass, methodName); if (methods.length == 0) { throw new MethodNotFoundException(String.format("Couldn't find a method with name %s in the class hierarchy of %s", methodName, declaringClass.getName())); } else if (methods.length > 1) { throw new TooManyMethodsFoundException(String.format("Found %d methods with name %s in the class hierarchy of %s.", methods.length, methodName, declaringClass.getName())); } MockRepository.putMethodToStub(methods[0], returnObject); }
@SuppressWarnings("unused") private void initializeInterceptionSettings() { MockPolicyInterceptionSettings interceptionSettings = getInterceptionSettings(); for (Method method : interceptionSettings.getMethodsToSuppress()) { MockRepository.addMethodToSuppress(method); } for (Entry<Method, InvocationHandler> entry : interceptionSettings.getProxiedMethods().entrySet()) { MockRepository.putMethodProxy(entry.getKey(), entry.getValue()); } for (Entry<Method, Object> entry : interceptionSettings.getStubbedMethods().entrySet()) { final Method method = entry.getKey(); final Object className = entry.getValue(); MockRepository.putMethodToStub(method, className); } for (Field field : interceptionSettings.getFieldsToSuppress()) { MockRepository.addFieldToSuppress(field); } for (String type : interceptionSettings.getFieldTypesToSuppress()) { MockRepository.addFieldTypeToSuppress(type); } }