示例#1
0
  /**
   * 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);
  }
示例#2
0
 /**
  * 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);
 }