public HandyReturnValues reportMatcher(Matcher matcher) { matcherStack.push(new LocalizedMatcher(matcher)); return new HandyReturnValues(); }
/** * Allows creating custom <code>Character</code> argument matchers. * <p> * See examples in javadoc for {@link Matchers} class * * @param matcher decides whether argument matches * @return <code>0</code>. */ public static char charThat(Matcher<Character> matcher) { return reportMatcher(matcher).returnChar(); }
/** * Allows creating custom <code>Boolean</code> argument matchers. * <p> * See examples in javadoc for {@link Matchers} class * * @param matcher decides whether argument matches * @return <code>false</code>. */ public static boolean booleanThat(Matcher<Boolean> matcher) { return reportMatcher(matcher).returnFalse(); }
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 new HandyReturnValues().returnFor(returnType); } else { return result; } }
/** * <code>null</code> argument. * <p> * See examples in javadoc for {@link Matchers} class * * @return <code>null</code>. */ public static Object isNull() { return reportMatcher(Null.NULL).returnNull(); }
/** * Allows creating custom <code>Short</code> argument matchers. * <p> * See examples in javadoc for {@link Matchers} class * * @param matcher decides whether argument matches * @return <code>0</code>. */ public static short shortThat(Matcher<Short> matcher) { return reportMatcher(matcher).returnZero(); }
public <T> T returnFor(T instance) { return instance == null ? null : (T) returnFor(instance.getClass()); } }
/** * Any <code>String</code> or <code>null</code>. * <p> * This method <b>*don't do any type checks*</b>, it is only there to avoid casting * in your code. This might however change (type checks could be added) in a * future major release. * <p> * See examples in javadoc for {@link Matchers} class * * @return empty String ("") */ public static String anyString() { return reportMatcher(Any.ANY).returnString(); }
/** * Any <code>List</code> or <code>null</code>. * <p> * This method <b>*don't do any type checks*</b>, it is only there to avoid casting * in your code. This might however change (type checks could be added) in a * future major release. * <p> * See examples in javadoc for {@link Matchers} class * * @return empty List. */ public static List anyList() { return reportMatcher(Any.ANY).returnList(); }
/** * Any <code>Map</code> or <code>null</code>. * <p> * This method <b>*don't do any type checks*</b>, it is only there to avoid casting * in your code. This might however change (type checks could be added) in a * future major release. * <p> * See examples in javadoc for {@link Matchers} class * * @return empty Map. */ public static Map anyMap() { return reportMatcher(Any.ANY).returnMap(); }
/** * Any <code>Set</code> or <code>null</code>. * <p> * This method <b>*don't do any type checks*</b>, it is only there to avoid casting * in your code. This might however change (type checks could be added) in a * future major release. * <p> * See examples in javadoc for {@link Matchers} class * * @return empty Set */ public static Set anySet() { return reportMatcher(Any.ANY).returnSet(); }
/** * Not <code>null</code> argument. * <p> * alias to {@link Matchers#isNotNull()} * <p> * See examples in javadoc for {@link Matchers} class * * @return <code>null</code>. */ public static Object notNull() { return reportMatcher(NotNull.NOT_NULL).returnNull(); }
/** * <code>short</code> argument that is equal to the given value. * <p> * See examples in javadoc for {@link Matchers} class * * @param value * the given value. * @return <code>0</code>. */ public static short eq(short value) { return reportMatcher(new Equals(value)).returnZero(); }
/** * 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 */ public T capture() { Mockito.argThat(capturingMatcher); return handyReturnValues.returnFor(clazz); }
/** * <code>String</code> argument that starts with the given prefix. * <p> * See examples in javadoc for {@link Matchers} class * * @param prefix * the prefix. * @return empty String (""). */ public static String startsWith(String prefix) { return reportMatcher(new StartsWith(prefix)).returnString(); }
/** * Any <code>Collection</code> or <code>null</code>. * <p> * This method <b>*don't do any type checks*</b>, it is only there to avoid casting * in your code. This might however change (type checks could be added) in a * future major release. * <p> * See examples in javadoc for {@link Matchers} class * * @return empty Collection. */ public static Collection anyCollection() { return reportMatcher(Any.ANY).returnList(); }
/** * Generic friendly alias to {@link Matchers#anyMap()}. * It's an alternative to @SuppressWarnings("unchecked") to keep code clean of compiler warnings. * <p> * Any <code>Map</code> or <code>null</code> * <p> * This method <b>*don't do any type checks*</b>, it is only there to avoid casting * in your code. This might however change (type checks could be added) in a * future major release. * <p> * See examples in javadoc for {@link Matchers} class * * @param keyClazz Type of the map key to avoid casting * @param valueClazz Type of the value to avoid casting * @return empty Map. */ public static <K, V> Map<K, V> anyMapOf(Class<K> keyClazz, Class<V> valueClazz) { return reportMatcher(Any.ANY).returnMap(); }
/** * Generic friendly alias to {@link Matchers#anySet()}. * It's an alternative to @SuppressWarnings("unchecked") to keep code clean of compiler warnings. * <p> * Any <code>Set</code> or <code>null</code> * <p> * This method <b>*don't do any type checks*</b>, it is only there to avoid casting * in your code. This might however change (type checks could be added) in a * future major release. * <p> * See examples in javadoc for {@link Matchers} class * * @param clazz Type owned by the Set to avoid casting * @return empty Set */ public static <T> Set<T> anySetOf(Class<T> clazz) { return (Set) reportMatcher(Any.ANY).returnSet(); }
/** * <code>null</code> argument. * The class argument is provided to avoid casting. * <p> * See examples in javadoc for {@link Matchers} class * * @param clazz Type to avoid casting * @return <code>null</code>. */ public static <T> T isNull(Class<T> clazz) { return (T) reportMatcher(Null.NULL).returnNull(); }
/** * int argument greater than or equal to the given value. * <p> * See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return <code>0</code>. */ public static int geq(int value) { return reportMatcher(new GreaterOrEqual<Integer>(value)).returnZero(); }