@Test public void getBestCandidateMethodReturnsMatchingMethodWhenNoOverloading() throws Exception { final Method expectedMethod = ClassWithStandardMethod.class.getDeclaredMethod("myMethod", double.class); final Method actualMethod = WhiteboxImpl.getBestMethodCandidate(ClassWithStandardMethod.class, "myMethod", new Class<?>[] { double.class }, false); assertThat(actualMethod).isEqualTo(expectedMethod); }
public static Method getBestMethodCandidate(Class<?> cls, String methodName, Class<?>[] signature, boolean exactParameterTypeMatch) { final Method foundMethod; final Method[] methods = getMethods(cls, methodName, signature, exactParameterTypeMatch); if (methods.length == 1) { foundMethod = methods[0]; } else { Arrays.sort(methods, ComparatorFactory.createMethodComparator()); foundMethod = methods[0]; } return foundMethod; }
WhiteboxImpl { public static Method getBestMethodCandidate(Class<?> cls, String methodName, Class<?>[] signature, boolean exactParameterTypeMatch) { final Method foundMethod; final Method[] methods = getMethods(cls, methodName, signature, exactParameterTypeMatch); if (methods.length == 1) { foundMethod = methods[0]; } else { Arrays.sort(methods, ComparatorFactory.createMethodComparator()); foundMethod = methods[0]; } return foundMethod; } }
WhiteboxImpl { public static Method getBestMethodCandidate(Class<?> cls, String methodName, Class<?>[] signature, boolean exactParameterTypeMatch) { final Method foundMethod; final Method[] methods = getMethods(cls, methodName, signature, exactParameterTypeMatch); if (methods.length == 1) { foundMethod = methods[0]; } else { Arrays.sort(methods, ComparatorFactory.createMethodComparator()); foundMethod = methods[0]; } return foundMethod; }  }
WhiteboxImpl { public static Method getBestMethodCandidate(Class<?> cls, String methodName, Class<?>[] signature, boolean exactParameterTypeMatch) { final Method foundMethod; final Method[] methods = getMethods(cls, methodName, signature, exactParameterTypeMatch); if (methods.length == 1) { foundMethod = methods[0]; } else { Arrays.sort(methods, ComparatorFactory.createMethodComparator()); foundMethod = methods[0]; } return foundMethod; }  static Method getMethod(Class<?> type, Class<?>... parameterTypes); static Method getMethod(Class<?> type, String methodName, Class<?>... parameterTypes); @SuppressWarnings({"unchecked", "rawtypes"}) static Field getField(Class<?> type, String fieldName); @SuppressWarnings("unchecked") static T newInstance(Class<T> classToInstantiate); static Constructor<?> getConstructor(Class<?> type, Class<?>... parameterTypes); static void setInternalState(Object object, String fieldName, Object value); static void setInternalState(Object object, String fieldName, Object[] value); static void setInternalState(Object object, Class<?> fieldType, Object value); static void setInternalState(Object object, Object value, Object... additionalValues); static void setInternalState(Object object, Object value, Class<?> where); static void setInternalState(Object object, Class<?> fieldType, Object value, Class<?> where); static void setInternalState(Object object, String fieldName, Object value, Class<?> where); @SuppressWarnings("unchecked") static T getInternalState(Object object, String fieldName); @SuppressWarnings("unchecked") static T getInternalState(Object object, Class<T> fieldType); @SuppressWarnings("unchecked") static T getInternalState(Object object, Class<T> fieldType, Class<?> where); @SuppressWarnings("unchecked") static T getInternalState(Object object, String fieldName, Class<?> where); @SuppressWarnings("unchecked") static synchronized T invokeMethod(Object tested, Object... arguments); @SuppressWarnings("unchecked") static synchronized T invokeMethod(Class<?> tested, Object... arguments); @SuppressWarnings("unchecked") static synchronized T invokeMethod(Object tested, String methodToExecute, Object... arguments); @SuppressWarnings("unchecked") static synchronized T invokeMethod(Object tested, String methodToExecute, Class<?>[] argumentTypes,
                                                  Object... arguments); @SuppressWarnings("unchecked") static synchronized T invokeMethod(Object tested, String methodToExecute, Class<?> definedIn,
                                                  Class<?>[] argumentTypes, Object... arguments); @SuppressWarnings("unchecked") static synchronized T invokeMethod(Object tested, Class<?> declaringClass, String methodToExecute,
                                                  Object... arguments); @SuppressWarnings("unchecked") static synchronized T invokeMethod(Object object, Class<?> declaringClass, String methodToExecute,
                                                  Class<?>[] parameterTypes, Object... arguments); @SuppressWarnings("unchecked") static synchronized T invokeMethod(Class<?> clazz, String methodToExecute, Object... arguments); static Method findMethodOrThrowException(Object tested, Class<?> declaringClass, String methodToExecute,
                                                    Object[] arguments); static Method getBestMethodCandidate(Class<?> cls, String methodName, Class<?>[] signature,
                                                boolean exactParameterTypeMatch); static Constructor<?> findDefaultConstructorOrThrowException(Class<?> type); static Constructor<?> findConstructorOrThrowException(Class<?> type); static Constructor<?> findUniqueConstructorOrThrowException(Class<?> type, Object... arguments); static void throwExceptionIfMethodWasNotFound(Class<?> type, String methodName, Method methodToMock,
                                                         Object... arguments); static void throwExceptionIfFieldWasNotFound(Class<?> type, String fieldName, Field field); static T invokeConstructor(Class<T> classThatContainsTheConstructorToTest, Class<?>[] parameterTypes,
                                          Object[] arguments); static T invokeConstructor(Class<T> classThatContainsTheConstructorToTest, Object... arguments); static Constructor<?>[] getAllConstructors(Class<?> clazz); static Method[] getAllMethods(Class<?> clazz); static Field[] getAllFields(Class<?> clazz); static Constructor<?> getFirstParentConstructor(Class<?> klass); static Method findMethod(Class<T> type, String methodName, Class<?>... parameterTypes); static Class<?> getOriginalUnmockedType(Class<T> type); static UnproxiedType getUnproxiedType(Class<T> type); @SuppressWarnings("all") static Method findMethodOrThrowException(Class<?> type, String methodName, Class<?>... parameterTypes); static Method[] getMethods(Class<?> clazz, String... methodNames); static Method[] getMethods(Class<?> clazz, String methodName, Class<?>[] expectedTypes,
                                      boolean exactParameterTypeMatch); static Field[] getFields(Class<?> clazz, String... fieldNames); @SuppressWarnings("unchecked") static T performMethodInvocation(Object tested, Method methodToInvoke, Object... arguments); static Method[] getAllMethodExcept(Class<T> type, String... methodNames); static Method[] getAllMethodsExcept(Class<T> type, String methodNameToExclude, Class<?>[] argumentTypes); static boolean areAllMethodsStatic(Method... methods); static Class<?> getType(Object object); static Class<?> getUnproxyType(Object object); @SuppressWarnings("unchecked") static Class<Object> getInnerClassType(Class<?> declaringClass, String name); @SuppressWarnings("unchecked") static Class<Object> getLocalClassType(Class<?> declaringClass, int occurrence, String name); @SuppressWarnings("unchecked") static Class<Object> getAnonymousInnerClassType(Class<?> declaringClass, int occurrence); @SuppressWarnings("unchecked") static Set<Field> getFieldsAnnotatedWith(Object object, Class<? extends Annotation> annotation,
                                                    Class<? extends Annotation>... additionalAnnotations); static Set<Field> getFieldsAnnotatedWith(Object object, Class<? extends Annotation>[] annotationTypes); static Set<Field> getFieldsOfType(Object object, Class<?> type); static Set<Field> getAllInstanceFields(Object object); static Set<Field> getAllStaticFields(Class<?> type); static boolean isClass(Object argument); static boolean checkIfParameterTypesAreSame(boolean isVarArgs, Class<?>[] expectedParameterTypes,
                                                       Class<?>[] actualParameterTypes); static void setInternalStateFromContext(Object object, Object context, Object[] additionalContexts); static void setInternalStateFromContext(Object object, Object context, FieldMatchingStrategy strategy); static void setInternalStateFromContext(Object object, Class<?> context, Class<?>[] additionalContexts); static void copyToMock(T from, T mock); static void copyToRealObject(T from, T to); }
WhiteboxImpl { public static Method getBestMethodCandidate(Class<?> cls, String methodName, Class<?>[] signature, boolean exactParameterTypeMatch) { final Method foundMethod; final Method[] methods = getMethods(cls, methodName, signature, exactParameterTypeMatch); if (methods.length == 1) { foundMethod = methods[0]; } else { Arrays.sort(methods, ComparatorFactory.createMethodComparator()); foundMethod = methods[0]; } return foundMethod; }  static Method getMethod(Class<?> type, Class<?>... parameterTypes); static Method getMethod(Class<?> type, String methodName, Class<?>... parameterTypes); @SuppressWarnings({"unchecked", "rawtypes"}) static Field getField(Class<?> type, String fieldName); @SuppressWarnings("unchecked") static T newInstance(Class<T> classToInstantiate); static Constructor<?> getConstructor(Class<?> type, Class<?>... parameterTypes); static void setInternalState(Object object, String fieldName, Object value); static void setInternalState(Object object, String fieldName, Object[] value); static void setInternalState(Object object, Class<?> fieldType, Object value); static void setInternalState(Object object, Object value, Object... additionalValues); static void setInternalState(Object object, Object value, Class<?> where); static void setInternalState(Object object, Class<?> fieldType, Object value, Class<?> where); static void setInternalState(Object object, String fieldName, Object value, Class<?> where); @SuppressWarnings("unchecked") static T getInternalState(Object object, String fieldName); @SuppressWarnings("unchecked") static T getInternalState(Object object, Class<T> fieldType); @SuppressWarnings("unchecked") static T getInternalState(Object object, Class<T> fieldType, Class<?> where); @SuppressWarnings("unchecked") static T getInternalState(Object object, String fieldName, Class<?> where); @SuppressWarnings("unchecked") static synchronized T invokeMethod(Object tested, Object... arguments); @SuppressWarnings("unchecked") static synchronized T invokeMethod(Class<?> tested, Object... arguments); @SuppressWarnings("unchecked") static synchronized T invokeMethod(Object tested, String methodToExecute, Object... arguments); @SuppressWarnings("unchecked") static synchronized T invokeMethod(Object tested, String methodToExecute, Class<?>[] argumentTypes,
                                                  Object... arguments); @SuppressWarnings("unchecked") static synchronized T invokeMethod(Object tested, String methodToExecute, Class<?> definedIn,
                                                  Class<?>[] argumentTypes, Object... arguments); @SuppressWarnings("unchecked") static synchronized T invokeMethod(Object tested, Class<?> declaringClass, String methodToExecute,
                                                  Object... arguments); @SuppressWarnings("unchecked") static synchronized T invokeMethod(Object object, Class<?> declaringClass, String methodToExecute,
                                                  Class<?>[] parameterTypes, Object... arguments); @SuppressWarnings("unchecked") static synchronized T invokeMethod(Class<?> clazz, String methodToExecute, Object... arguments); static Method findMethodOrThrowException(Object tested, Class<?> declaringClass, String methodToExecute,
                                                    Object[] arguments); static Method getBestMethodCandidate(Class<?> cls, String methodName, Class<?>[] signature,
                                                boolean exactParameterTypeMatch); static Constructor<?> findDefaultConstructorOrThrowException(Class<?> type); static Constructor<?> findConstructorOrThrowException(Class<?> type); static Constructor<?> findUniqueConstructorOrThrowException(Class<?> type, Object... arguments); static void throwExceptionIfMethodWasNotFound(Class<?> type, String methodName, Method methodToMock,
                                                         Object... arguments); static void throwExceptionIfFieldWasNotFound(Class<?> type, String fieldName, Field field); static T invokeConstructor(Class<T> classThatContainsTheConstructorToTest, Class<?>[] parameterTypes,
                                          Object[] arguments); static T invokeConstructor(Class<T> classThatContainsTheConstructorToTest, Object... arguments); static Constructor<?>[] getAllConstructors(Class<?> clazz); static Method[] getAllMethods(Class<?> clazz); static Field[] getAllFields(Class<?> clazz); static Constructor<?> getFirstParentConstructor(Class<?> klass); static Method findMethod(Class<T> type, String methodName, Class<?>... parameterTypes); static Class<?> getOriginalUnmockedType(Class<T> type); static UnproxiedType getUnproxiedType(Class<T> type); @SuppressWarnings("all") static Method findMethodOrThrowException(Class<?> type, String methodName, Class<?>... parameterTypes); static Method[] getMethods(Class<?> clazz, String... methodNames); static Method[] getMethods(Class<?> clazz, String methodName, Class<?>[] expectedTypes,
                                      boolean exactParameterTypeMatch); static Field[] getFields(Class<?> clazz, String... fieldNames); @SuppressWarnings("unchecked") static T performMethodInvocation(Object tested, Method methodToInvoke, Object... arguments); static Method[] getAllMethodExcept(Class<T> type, String... methodNames); static Method[] getAllMethodsExcept(Class<T> type, String methodNameToExclude, Class<?>[] argumentTypes); static boolean areAllMethodsStatic(Method... methods); static Class<?> getType(Object object); static Class<?> getUnproxyType(Object object); @SuppressWarnings("unchecked") static Class<Object> getInnerClassType(Class<?> declaringClass, String name); @SuppressWarnings("unchecked") static Class<Object> getLocalClassType(Class<?> declaringClass, int occurrence, String name); @SuppressWarnings("unchecked") static Class<Object> getAnonymousInnerClassType(Class<?> declaringClass, int occurrence); @SuppressWarnings("unchecked") static Set<Field> getFieldsAnnotatedWith(Object object, Class<? extends Annotation> annotation,
                                                    Class<? extends Annotation>... additionalAnnotations); static Set<Field> getFieldsAnnotatedWith(Object object, Class<? extends Annotation>[] annotationTypes); static Set<Field> getFieldsOfType(Object object, Class<?> type); static Set<Field> getAllInstanceFields(Object object); static Set<Field> getAllStaticFields(Class<?> type); static boolean isClass(Object argument); static boolean checkIfParameterTypesAreSame(boolean isVarArgs, Class<?>[] expectedParameterTypes,
                                                       Class<?>[] actualParameterTypes); static void setInternalStateFromContext(Object object, Object context, Object[] additionalContexts); static void setInternalStateFromContext(Object object, Object context, FieldMatchingStrategy strategy); static void setInternalStateFromContext(Object object, Class<?> context, Class<?>[] additionalContexts); static void copyToMock(T from, T mock); static void copyToRealObject(T from, T to);  }