/** * Use it to capture the argument. This method <b>must be used inside of verification</b>. * <p> * Internally, this method registers a special implementation of an {@link ArgumentMatcher}. * This argument matcher stores the argument value so that you can use it later to perform assertions. * <p> * See examples in javadoc for {@link ArgumentCaptor} class. * * @return null or default values */ public T capture() { Mockito.argThat(capturingMatcher); return defaultValue(clazz); }
public static boolean isAssignableFromWrapper(Class<?> valueClass, Class<?> referenceType) { if(isPrimitiveOrWrapper(valueClass) && isPrimitiveOrWrapper(referenceType)) { return Primitives.primitiveTypeOf(valueClass).isAssignableFrom(Primitives.primitiveTypeOf(referenceType)); } return false; }
public <T> T returnFor(Class<T> clazz) { // explicitly return null if type is not a primitive or a wrapper if (isPrimitiveOrWrapper(clazz)) { return defaultValueForPrimitiveOrWrapper(clazz); } return null; }
public boolean isValidReturnType(Class<?> clazz) { if (method.getReturnType().isPrimitive() || clazz.isPrimitive()) { return Primitives.primitiveTypeOf(clazz) == Primitives.primitiveTypeOf(method.getReturnType()); } else { return method.getReturnType().isAssignableFrom(clazz); } }
Object returnValueFor(Class<?> type) { if (Primitives.isPrimitiveOrWrapper(type)) { return Primitives.defaultValue(type);
public <T> T returnFor(Class<T> clazz) { if (isPrimitiveWrapper(clazz)) { return primitiveWrapperOf(clazz); } return primitiveValueOrNullFor(clazz); }
Object returnValueFor(Class<?> type) { if (type.isPrimitive()) { return primitiveOf(type); } else if (Primitives.isPrimitiveWrapper(type)) { return Primitives.primitiveWrapperOf(type);
public boolean matches(Object actual) { return (actual != null) && (Primitives.isAssignableFromWrapper(actual.getClass(), clazz) || clazz.isAssignableFrom(actual.getClass())); }
public boolean isValidReturnType(Class clazz) { if (method.getReturnType().isPrimitive() || clazz.isPrimitive()) { return Primitives.primitiveTypeOf(clazz) == Primitives.primitiveTypeOf(method.getReturnType()); } else { return method.getReturnType().isAssignableFrom(clazz); } }
@Override public Object handle(Invocation invocation) throws Throwable { Object result = delegate.handle(invocation); Class<?> returnType = invocation.getMethod().getReturnType(); if(result == null && returnType.isPrimitive()) { //primitive values cannot be null return defaultValue(returnType); } return result; }
private static boolean paramsMatch(Class<?>[] types, Object[] params) { if (params.length != types.length) { return false; } for (int i = 0; i < params.length; i++) { if (params[i] == null) { if (types[i].isPrimitive()) { return false; } } else if ((!types[i].isPrimitive() && !types[i].isInstance(params[i])) || (types[i].isPrimitive() && !types[i].equals(Primitives.primitiveTypeOf(params[i].getClass())))) { return false; } } return true; }
Object returnValueFor(Class<?> type) { if (Primitives.isPrimitiveOrWrapper(type)) { return Primitives.defaultValueForPrimitiveOrWrapper(type);
/** * Allows matching arguments with hamcrest matchers. * <p/> * See examples in javadoc for {@link MockitoHamcrest} class * * @param matcher decides whether argument matches * @return <code>null</code> or default value for primitive (0, false, etc.) * @since 2.1.0 */ @SuppressWarnings("unchecked") public static <T> T argThat(Matcher<T> matcher) { reportMatcher(matcher); return (T) defaultValue(genericTypeOfMatcher(matcher.getClass())); }
public boolean isValidReturnType(Class clazz) { if (method.getReturnType().isPrimitive()) { return Primitives.primitiveTypeOf(clazz) == method.getReturnType(); } else { return method.getReturnType().isAssignableFrom(clazz); } }
/** * Object argument that is equal to the given value. * * <p> * See examples in javadoc for {@link ArgumentMatchers} class * </p> * * @param value the given value. * @return <code>null</code>. */ public static <T> T eq(T value) { reportMatcher(new Equals(value)); if (value == null) return null; return (T) Primitives.defaultValue(value.getClass()); }
/** * Object argument that is the same as the given value. * * <p> * See examples in javadoc for {@link ArgumentMatchers} class * </p> * * @param <T> the type of the object, it is passed through to prevent casts. * @param value the given value. * @return <code>null</code>. */ public static <T> T same(T value) { reportMatcher(new Same(value)); if (value == null) return null; return (T) Primitives.defaultValue(value.getClass()); }
/** * <code>Object</code> argument that implements the given class. * <p> * See examples in javadoc for {@link ArgumentMatchers} class * * @param <T> the accepted type. * @param type the class of the accepted type. * @return <code>null</code>. * @see #any(Class) */ public static <T> T isA(Class<T> type) { reportMatcher(new InstanceOf(type)); return defaultValue(type); }
return defaultValue(type);
/** * Argument that is either <code>null</code> or of the given type. * * <p> * See examples in javadoc for {@link ArgumentMatchers} class * </p> * * @param clazz Type to avoid casting * @return <code>null</code>. */ public static <T> T nullable(Class<T> clazz) { AdditionalMatchers.or(isNull(), isA(clazz)); return (T) Primitives.defaultValue(clazz); }
@SuppressWarnings("unchecked") private <T> T internalReturnForLambda(Object consumer, Class consumerType) { Class<?>[] typeArgs = TypeResolver.resolveRawArguments(consumerType, consumer.getClass()); return (T) Primitives.defaultValue(typeArgs[0]); } }