/** * 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); }
/** * Throw exception if method was not found. * * @param type the type * @param methodName the method name * @param methodToMock the method to mock * @param arguments the arguments */ public static void throwExceptionIfMethodWasNotFound(Class<?> type, String methodName, Method methodToMock, Object... arguments) { if (methodToMock == null) { String methodNameData = ""; if (methodName != null) { methodNameData = "with name '" + methodName + "' "; } throw new MethodNotFoundException("No method found " + methodNameData + "with parameter types: [ " + getArgumentTypesAsString(arguments) + " ] in class " + getOriginalUnmockedType(type) .getName() + "."); } }
/** * Throw exception if method was not found. * * @param type * the type * @param methodName * the method name * @param methodToMock * the method to mock * @param arguments * the arguments */ public static void throwExceptionIfMethodWasNotFound(Class<?> type, String methodName, Method methodToMock, Object... arguments) { if (methodToMock == null) { String methodNameData = ""; if (methodName != null) { methodNameData = "with name '" + methodName + "' "; } throw new MethodNotFoundException("No method found " + methodNameData + "with parameter types: [ " + getArgumentTypesAsString(arguments) + " ] in class " + getUnmockedType(type).getName() + "."); } }
/** * Add a proxy for a method declared in class {@code declaringClass}. * Each call to the method will be routed to the invocationHandler instead. */ public static void proxy(Class<?> declaringClass, String methodName, InvocationHandler invocationHandler) { assertInvocationHandlerNotNull(invocationHandler); 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.putMethodProxy(methods[0], invocationHandler); }
/** * Add a proxy for a method declared in class <code>declaringClass</code>. * Each call to the method will be routed to the invocationHandler instead. */ public static void proxy(Class<?> declaringClass, String methodName, InvocationHandler invocationHandler) { assertInvocationHandlerNotNull(invocationHandler); 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.putMethodProxy(methods[0], invocationHandler); }
throw new MethodNotFoundException(String.format( "No methods matching the name(s) %s were found in the class hierarchy of %s.", concatenateStrings(methodNames), getType(clazz)));
throw new MethodNotFoundException(String.format( "No methods matching the name(s) %s were found in the class hierarchy of %s.", concatenateStrings(methodName), getType(clazz)));
throw new MethodNotFoundException(String.format( "No methods matching the name(s) %s were found in the class hierarchy of %s.", concatenateStrings(methodNames), getType(clazz)));
throw new MethodNotFoundException("No method was found with parameter types: [ " + getArgumentTypesAsString((Object[]) parameterTypes) + " ] in class " + getUnmockedType(type).getName() + ".");
throw new MethodNotFoundException("No method was found with parameter types: [ " + getArgumentTypesAsString((Object[]) parameterTypes) + " ] in class " + getOriginalUnmockedType(type).getName() + ".");
throw new MethodNotFoundException(String.format( "No methods matching the name(s) %s were found in the class hierarchy of %s.", concatenateStrings(methodName), getType(clazz)));