@Override public void notFound(Class<?> type, boolean isInstanceField) throws FieldNotFoundException { throw new FieldNotFoundException(String.format("No %s field named \"%s\" could be found in the class hierarchy of %s.", isInstanceField ? "instance" : "static", fieldName, type.getName())); }
/** * Get the first parent constructor defined in a super class of * {@code klass}. * * @param klass The class where the constructor is located. {@code null} * ). * @return A . */ public static Constructor<?> getFirstParentConstructor(Class<?> klass) { try { return getOriginalUnmockedType(klass).getSuperclass().getDeclaredConstructors()[0]; } catch (Exception e) { throw new ConstructorNotFoundException("Failed to lookup constructor.", e); } }
/** * 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); }
private static <T> Constructor<T> getBestCandidateConstructor(Class<T> classThatContainsTheConstructorToTest, Class<?>[] argumentTypes, Object[] arguments) { Constructor<T> constructor; Constructor<T> potentialConstructorWrapped = getPotentialConstructorWrapped(classThatContainsTheConstructorToTest, argumentTypes); Constructor<T> potentialConstructorPrimitive = getPotentialConstructorPrimitive(classThatContainsTheConstructorToTest, argumentTypes); if (potentialConstructorPrimitive == null && potentialConstructorWrapped == null) { // Check if we can find a matching var args constructor. constructor = getPotentialVarArgsConstructor(classThatContainsTheConstructorToTest, arguments); if (constructor == null) { throw new ConstructorNotFoundException("Failed to find a constructor with parameter types: [" + getArgumentTypesAsString(arguments) + "]"); } } else if (potentialConstructorPrimitive == null) { constructor = potentialConstructorWrapped; } else if (potentialConstructorWrapped == null) { constructor = potentialConstructorPrimitive; } else if (arguments == null || arguments.length == 0 && potentialConstructorPrimitive != null) { constructor = potentialConstructorPrimitive; } else { throw new TooManyConstructorsFoundException( "Could not determine which constructor to execute. Please specify the parameter types by hand."); } return constructor; }
/** * 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() + "."); } }
@Override public void notFound(Class<?> type, boolean isInstanceField) throws FieldNotFoundException { throw new FieldNotFoundException(String.format("No %s fields were declared in %s.", isInstanceField ? "instance" : "static", type.getName())); } }
/** * Get the first parent constructor defined in a super class of * <code>klass</code>. * * @param klass * The class where the constructor is located. <code>null</code> * ). * @return A . */ public static Constructor<?> getFirstParentConstructor(Class<?> klass) { try { return getUnmockedType(klass).getSuperclass().getDeclaredConstructors()[0]; } catch (Exception e) { throw new ConstructorNotFoundException("Failed to lookup constructor.", e); } }
@Override public void notFound(Class<?> type, boolean isInstanceField) throws FieldNotFoundException { throw new FieldNotFoundException(String.format("No %s field of type \"%s\" could be found in the class hierarchy of %s.", isInstanceField ? "instance" : "static", expectedFieldType.getName(), type.getName())); }
private void throwExceptionIfConstructorWasNotFound() { if (potentialConstructor == null) { String message = "No constructor found in class '" + WhiteboxImpl.getOriginalUnmockedType(type).getName() + "' " + "with " + "parameter types: [ " + WhiteboxImpl.getArgumentTypesAsString(arguments) + " ]."; throw new ConstructorNotFoundException(message); } } }
@Override public void notFound(Class<?> type, boolean isInstanceField) throws FieldNotFoundException { throw new FieldNotFoundException(String.format("No %s field assignable from \"%s\" could be found in the class hierarchy of %s.", isInstanceField ? "instance" : "static", expectedFieldType.getName(), type.getName())); }
/** * Throw exception if constructor was not found. * * @param type the type * @param potentialConstructor the potential constructor * @param arguments the arguments */ static void throwExceptionIfConstructorWasNotFound(Class<?> type, Constructor<?> potentialConstructor, Object... arguments) { if (potentialConstructor == null) { String message = "No constructor found in class '" + getOriginalUnmockedType(type).getName() + "' with " + "parameter types: [ " + getArgumentTypesAsString(arguments) + " ]."; throw new ConstructorNotFoundException(message); } }
@Override public void notFound(Class<?> type, boolean isInstanceField) throws FieldNotFoundException { throw new FieldNotFoundException(String.format("No %s fields were declared in %s.", isInstanceField ? "instance" : "static", type.getName())); } }
/** * Throw exception if constructor was not found. * * @param type * the type * @param potentialConstructor * the potential constructor * @param arguments * the arguments */ static void throwExceptionIfConstructorWasNotFound(Class<?> type, Constructor<?> potentialConstructor, Object... arguments) { if (potentialConstructor == null) { String message = "No constructor found in class '" + getUnmockedType(type).getName() + "' with " + "parameter types: [ " + getArgumentTypesAsString(arguments) + " ]."; throw new ConstructorNotFoundException(message); } }
@Override public void notFound(Class<?> type, boolean isInstanceField) throws FieldNotFoundException { throw new FieldNotFoundException(String.format("No %s field named \"%s\" could be found in the class hierarchy of %s.", isInstanceField ? "instance" : "static", fieldName, type.getName())); }
@Override public void notFound(Class<?> type, boolean isInstanceField) throws FieldNotFoundException { throw new FieldNotFoundException(String.format("No %s field assignable from \"%s\" could be found in the class hierarchy of %s.", isInstanceField ? "instance" : "static", expectedFieldType.getName(), type.getName())); }
@Override public void notFound(Class<?> type, boolean isInstanceField) throws FieldNotFoundException { throw new FieldNotFoundException(String.format("No %s field of type \"%s\" could be found in the class hierarchy of %s.", isInstanceField ? "instance" : "static", expectedFieldType.getName(), type.getName())); }
@Override public void notFound(Class<?> type, boolean isInstanceField) throws FieldNotFoundException { throw new FieldNotFoundException("No field that has any of the annotation types \"" + getAnnotationNames() + "\" could be found in the class hierarchy of " + type.getName() + "."); }
@Override public void notFound(Class<?> type, boolean isInstanceField) throws FieldNotFoundException { throw new FieldNotFoundException("No field that has any of the annotation types \"" + getAnnotationNames() + "\" could be found in the class hierarchy of " + type.getName() + "."); }
/** * Throw exception if field was not found. * * @param type the type * @param fieldName the field name * @param field the field */ public static void throwExceptionIfFieldWasNotFound(Class<?> type, String fieldName, Field field) { if (field == null) { throw new FieldNotFoundException("No field was found with name '" + fieldName + "' in class " + getOriginalUnmockedType(type).getName() + "."); } }
/** * Throw exception if field was not found. * * @param type * the type * @param fieldName * the field name * @param field * the field */ public static void throwExceptionIfFieldWasNotFound(Class<?> type, String fieldName, Field field) { if (field == null) { throw new FieldNotFoundException("No field was found with name '" + fieldName + "' in class " + getUnmockedType(type).getName() + "."); } }