Package org.mockito

Class ArgumentMatchers

  • Direct Known Subclasses:
    Matchers, Mockito

    public class ArgumentMatchers
    extends java.lang.Object
    Allow flexible verification or stubbing. See also AdditionalMatchers.

    Mockito extends ArgumentMatchers so to get access to all matchers just import Mockito class statically.

    
     //stubbing using anyInt() argument matcher
     when(mockedList.get(anyInt())).thenReturn("element");
    
     //following prints "element"
     System.out.println(mockedList.get(999));
    
     //you can also verify using argument matcher
     verify(mockedList).get(anyInt());
     

    Since Mockito any(Class) and anyInt family matchers perform a type check, thus they won't match null arguments. Instead use the isNull matcher.

    
     // stubbing using anyBoolean() argument matcher
     when(mock.dryRun(anyBoolean())).thenReturn("state");
    
     // below the stub won't match, and won't return "state"
     mock.dryRun(null);
    
     // either change the stub
     when(mock.dryRun(isNull())).thenReturn("state");
     mock.dryRun(null); // ok
    
     // or fix the code ;)
     when(mock.dryRun(anyBoolean())).thenReturn("state");
     mock.dryRun(true); // ok
    
     
    The same apply for verification.

    Scroll down to see all methods - full list of matchers.

    Warning:
    If you are using argument matchers, all arguments have to be provided by matchers. E.g: (example shows verification but the same applies to stubbing):

    
     verify(mock).someMethod(anyInt(), anyString(), eq("third argument"));
     //above is correct - eq() is also an argument matcher
    
     verify(mock).someMethod(anyInt(), anyString(), "third argument");
     //above is incorrect - exception will be thrown because third argument is given without argument matcher.
     

    Matcher methods like anyObject(), eq() do not return matchers. Internally, they record a matcher on a stack and return a dummy value (usually null). This implementation is due to static type safety imposed by java compiler. The consequence is that you cannot use anyObject(), eq() methods outside of verified/stubbed method.

    Additional matchers

    The class AdditionalMatchers offers rarely used matchers, although they can be useful, when it is useful to combine multiple matchers or when it is useful to negate a matcher necessary.

    Custom Argument ArgumentMatchers

    It is important to understand the use cases and available options for dealing with non-trivial arguments before implementing custom argument matchers. This way, you can select the best possible approach for given scenario and produce highest quality test (clean and maintainable). Please read on in the javadoc for ArgumentMatcher to learn about approaches and see the examples.

    See Also:
    AdditionalMatchers
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static <T> T any()
      Matches anything, including nulls and varargs.
      static <T> T any​(java.lang.Class<T> type)
      Matches any object of given type, excluding nulls.
      static boolean anyBoolean()
      Any boolean or non-null Boolean
      static byte anyByte()
      Any byte or non-null Byte.
      static char anyChar()
      Any char or non-null Character.
      static <T> java.util.Collection<T> anyCollection()
      Any non-null Collection.
      static <T> java.util.Collection<T> anyCollectionOf​(java.lang.Class<T> clazz)
      Deprecated.
      With Java 8 this method will be removed in Mockito 4.0.
      static double anyDouble()
      Any double or non-null Double.
      static float anyFloat()
      Any float or non-null Float.
      static int anyInt()
      Any int or non-null Integer.
      static <T> java.lang.Iterable<T> anyIterable()
      Any non-null Iterable.
      static <T> java.lang.Iterable<T> anyIterableOf​(java.lang.Class<T> clazz)
      Deprecated.
      With Java 8 this method will be removed in Mockito 4.0.
      static <T> java.util.List<T> anyList()
      Any non-null List.
      static <T> java.util.List<T> anyListOf​(java.lang.Class<T> clazz)
      Deprecated.
      With Java 8 this method will be removed in Mockito 4.0.
      static long anyLong()
      Any long or non-null Long.
      static <K,​V>
      java.util.Map<K,​V>
      anyMap()
      Any non-null Map.
      static <K,​V>
      java.util.Map<K,​V>
      anyMapOf​(java.lang.Class<K> keyClazz, java.lang.Class<V> valueClazz)
      Deprecated.
      With Java 8 this method will be removed in Mockito 4.0.
      static <T> T anyObject()
      Deprecated.
      This will be removed in Mockito 4.0 This method is only used for generic friendliness to avoid casting, this is not anymore needed in Java 8.
      static <T> java.util.Set<T> anySet()
      Any non-null Set.
      static <T> java.util.Set<T> anySetOf​(java.lang.Class<T> clazz)
      Deprecated.
      With Java 8 this method will be removed in Mockito 4.0.
      static short anyShort()
      Any short or non-null Short.
      static java.lang.String anyString()
      Any non-null String
      static <T> T anyVararg()
      Deprecated.
      as of 2.1.0 use any()
      static <T> T argThat​(ArgumentMatcher<T> matcher)
      Allows creating custom argument matchers.
      static boolean booleanThat​(ArgumentMatcher<java.lang.Boolean> matcher)
      Allows creating custom boolean argument matchers.
      static byte byteThat​(ArgumentMatcher<java.lang.Byte> matcher)
      Allows creating custom byte argument matchers.
      static char charThat​(ArgumentMatcher<java.lang.Character> matcher)
      Allows creating custom char argument matchers.
      static java.lang.String contains​(java.lang.String substring)
      String argument that contains the given substring.
      static double doubleThat​(ArgumentMatcher<java.lang.Double> matcher)
      Allows creating custom double argument matchers.
      static java.lang.String endsWith​(java.lang.String suffix)
      String argument that ends with the given suffix.
      static boolean eq​(boolean value)
      boolean argument that is equal to the given value.
      static byte eq​(byte value)
      byte argument that is equal to the given value.
      static char eq​(char value)
      char argument that is equal to the given value.
      static double eq​(double value)
      double argument that is equal to the given value.
      static float eq​(float value)
      float argument that is equal to the given value.
      static int eq​(int value)
      int argument that is equal to the given value.
      static long eq​(long value)
      long argument that is equal to the given value.
      static short eq​(short value)
      short argument that is equal to the given value.
      static <T> T eq​(T value)
      Object argument that is equal to the given value.
      static float floatThat​(ArgumentMatcher<java.lang.Float> matcher)
      Allows creating custom float argument matchers.
      static int intThat​(ArgumentMatcher<java.lang.Integer> matcher)
      Allows creating custom int argument matchers.
      static <T> T isA​(java.lang.Class<T> type)
      Object argument that implements the given class.
      static <T> T isNotNull()
      Not null argument.
      static <T> T isNotNull​(java.lang.Class<T> clazz)
      Deprecated.
      With Java 8 this method will be removed in Mockito 4.0.
      static <T> T isNull()
      null argument.
      static <T> T isNull​(java.lang.Class<T> clazz)
      Deprecated.
      With Java 8 this method will be removed in Mockito 4.0.
      static long longThat​(ArgumentMatcher<java.lang.Long> matcher)
      Allows creating custom long argument matchers.
      static java.lang.String matches​(java.lang.String regex)
      String argument that matches the given regular expression.
      static java.lang.String matches​(java.util.regex.Pattern pattern)
      Pattern argument that matches the given regular expression.
      static <T> T notNull()
      Not null argument.
      static <T> T notNull​(java.lang.Class<T> clazz)
      Deprecated.
      With Java 8 this method will be removed in Mockito 4.0.
      static <T> T nullable​(java.lang.Class<T> clazz)
      Argument that is either null or of the given type.
      static <T> T refEq​(T value, java.lang.String... excludeFields)
      Object argument that is reflection-equal to the given value with support for excluding selected fields from a class.
      private static void reportMatcher​(ArgumentMatcher<?> matcher)  
      static <T> T same​(T value)
      Object argument that is the same as the given value.
      static short shortThat​(ArgumentMatcher<java.lang.Short> matcher)
      Allows creating custom short argument matchers.
      static java.lang.String startsWith​(java.lang.String prefix)
      String argument that starts with the given prefix.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ArgumentMatchers

        public ArgumentMatchers()
    • Method Detail

      • anyObject

        @Deprecated
        public static <T> T anyObject()
        Deprecated.
        This will be removed in Mockito 4.0 This method is only used for generic friendliness to avoid casting, this is not anymore needed in Java 8.
        Matches anything, including null.

        This is an alias of: any(). See examples in javadoc for ArgumentMatchers class.

        Returns:
        null.
        See Also:
        any(), any(Class), notNull(), notNull(Class)
      • any

        public static <T> T any​(java.lang.Class<T> type)
        Matches any object of given type, excluding nulls.

        This matcher will perform a type check with the given type, thus excluding values. See examples in javadoc for ArgumentMatchers class. This is an alias of: isA(Class)}

        Since Mockito 2.1.0, only allow non-null instance of , thus null is not anymore a valid value. As reference are nullable, the suggested API to match null would be isNull(). We felt this change would make test harnesses much safer than they were with Mockito 1.x.

        Notes :

        • For primitive types use anyChar() family.
        • Since Mockito 2.1.0 this method will perform a type check thus null values are not authorized.
        • Since mockito 2.1.0 any() and anyObject() are not anymore aliases of this method.

        Type Parameters:
        T - The accepted type
        Parameters:
        type - the class of the accepted type.
        Returns:
        null.
        See Also:
        any(), anyObject(), anyVararg(), isA(Class), notNull(), notNull(Class), isNull(), isNull(Class)
      • isA

        public static <T> T isA​(java.lang.Class<T> type)
        Object argument that implements the given class.

        See examples in javadoc for ArgumentMatchers class

        Type Parameters:
        T - the accepted type.
        Parameters:
        type - the class of the accepted type.
        Returns:
        null.
        See Also:
        any(Class)
      • anyVararg

        @Deprecated
        public static <T> T anyVararg()
        Deprecated.
        as of 2.1.0 use any()
        Any vararg, meaning any number and values of arguments.

        Example:

        
         //verification:
         mock.foo(1, 2);
         mock.foo(1, 2, 3, 4);
        
         verify(mock, times(2)).foo(anyVararg());
        
         //stubbing:
         when(mock.foo(anyVararg()).thenReturn(100);
        
         //prints 100
         System.out.println(mock.foo(1, 2));
         //also prints 100
         System.out.println(mock.foo(1, 2, 3, 4));
         

        See examples in javadoc for ArgumentMatchers class.

        Returns:
        null.
        See Also:
        any(), any(Class)
      • anyBoolean

        public static boolean anyBoolean()
        Any boolean or non-null Boolean

        Since Mockito 2.1.0, only allow valued Boolean, thus null is not anymore a valid value. As primitive wrappers are nullable, the suggested API to match null wrapper would be isNull(). We felt this change would make test harnesses much safer than they were with Mockito 1.x.

        See examples in javadoc for ArgumentMatchers class.

        Returns:
        false.
        See Also:
        isNull(), isNull(Class)
      • anyByte

        public static byte anyByte()
        Any byte or non-null Byte.

        Since Mockito 2.1.0, only allow valued Byte, thus null is not anymore a valid value. As primitive wrappers are nullable, the suggested API to match null wrapper would be isNull(). We felt this change would make test harnesses much safer than they were with Mockito 1.x.

        See examples in javadoc for ArgumentMatchers class.

        Returns:
        0.
        See Also:
        isNull(), isNull(Class)
      • anyChar

        public static char anyChar()
        Any char or non-null Character.

        Since Mockito 2.1.0, only allow valued Character, thus null is not anymore a valid value. As primitive wrappers are nullable, the suggested API to match null wrapper would be isNull(). We felt this change would make test harnesses much safer than they were with Mockito 1.x.

        See examples in javadoc for ArgumentMatchers class.

        Returns:
        0.
        See Also:
        isNull(), isNull(Class)
      • anyInt

        public static int anyInt()
        Any int or non-null Integer.

        Since Mockito 2.1.0, only allow valued Integer, thus null is not anymore a valid value. As primitive wrappers are nullable, the suggested API to match null wrapper would be isNull(). We felt this change would make test harnesses much safer than they were with Mockito 1.x.

        See examples in javadoc for ArgumentMatchers class.

        Returns:
        0.
        See Also:
        isNull(), isNull(Class)
      • anyLong

        public static long anyLong()
        Any long or non-null Long.

        Since Mockito 2.1.0, only allow valued Long, thus null is not anymore a valid value. As primitive wrappers are nullable, the suggested API to match null wrapper would be isNull(). We felt this change would make test harnesses much safer than they were with Mockito 1.x.

        See examples in javadoc for ArgumentMatchers class.

        Returns:
        0.
        See Also:
        isNull(), isNull(Class)
      • anyFloat

        public static float anyFloat()
        Any float or non-null Float.

        Since Mockito 2.1.0, only allow valued Float, thus null is not anymore a valid value. As primitive wrappers are nullable, the suggested API to match null wrapper would be isNull(). We felt this change would make test harnesses much safer than they were with Mockito 1.x.

        See examples in javadoc for ArgumentMatchers class.

        Returns:
        0.
        See Also:
        isNull(), isNull(Class)
      • anyDouble

        public static double anyDouble()
        Any double or non-null Double.

        Since Mockito 2.1.0, only allow valued Double, thus null is not anymore a valid value. As primitive wrappers are nullable, the suggested API to match null wrapper would be isNull(). We felt this change would make test harnesses much safer than they were with Mockito 1.x.

        See examples in javadoc for ArgumentMatchers class.

        Returns:
        0.
        See Also:
        isNull(), isNull(Class)
      • anyShort

        public static short anyShort()
        Any short or non-null Short.

        Since Mockito 2.1.0, only allow valued Short, thus null is not anymore a valid value. As primitive wrappers are nullable, the suggested API to match null wrapper would be isNull(). We felt this change would make test harnesses much safer than they were with Mockito 1.x.

        See examples in javadoc for ArgumentMatchers class.

        Returns:
        0.
        See Also:
        isNull(), isNull(Class)
      • anyString

        public static java.lang.String anyString()
        Any non-null String

        Since Mockito 2.1.0, only allow non-null String. As this is a nullable reference, the suggested API to match null wrapper would be isNull(). We felt this change would make test harnesses much safer than they were with Mockito 1.x.

        See examples in javadoc for ArgumentMatchers class.

        Returns:
        empty String ("")
        See Also:
        isNull(), isNull(Class)
      • anyList

        public static <T> java.util.List<T> anyList()
        Any non-null List.

        Since Mockito 2.1.0, only allow non-null List. As this is a nullable reference, the suggested API to match null wrapper would be isNull(). We felt this change would make test harnesses much safer than they were with Mockito 1.x.

        See examples in javadoc for ArgumentMatchers class.

        Returns:
        empty List.
        See Also:
        anyListOf(Class), isNull(), isNull(Class)
      • anyListOf

        @Deprecated
        public static <T> java.util.List<T> anyListOf​(java.lang.Class<T> clazz)
        Deprecated.
        With Java 8 this method will be removed in Mockito 4.0. This method is only used for generic friendliness to avoid casting, this is not anymore needed in Java 8.
        Any non-null List. Generic friendly alias to anyList(). It's an alternative to @SuppressWarnings("unchecked") to keep code clean of compiler warnings.

        This method doesn't do type checks of the list content with the given type parameter, it is only there to avoid casting in the code.

        Since Mockito 2.1.0, only allow non-null List. As this is a nullable reference, the suggested API to match null wrapper would be isNull(). We felt this change would make test harnesses much safer than they were with Mockito 1.x.

        See examples in javadoc for ArgumentMatchers class.

        Parameters:
        clazz - Type owned by the list to avoid casting
        Returns:
        empty List.
        See Also:
        anyList(), isNull(), isNull(Class)
      • anySet

        public static <T> java.util.Set<T> anySet()
        Any non-null Set.

        Since Mockito 2.1.0, only allow non-null Set. As this is a nullable reference, the suggested API to match null wrapper would be isNull(). We felt this change would make test harnesses much safer than they were with Mockito 1.x.

        See examples in javadoc for ArgumentMatchers class.

        Returns:
        empty Set
        See Also:
        anySetOf(Class), isNull(), isNull(Class)
      • anySetOf

        @Deprecated
        public static <T> java.util.Set<T> anySetOf​(java.lang.Class<T> clazz)
        Deprecated.
        With Java 8 this method will be removed in Mockito 4.0. This method is only used for generic friendliness to avoid casting, this is not anymore needed in Java 8.
        Any non-null Set.

        Generic friendly alias to anySet(). It's an alternative to @SuppressWarnings("unchecked") to keep code clean of compiler warnings.

        This method doesn't do type checks of the set content with the given type parameter, it is only there to avoid casting in the code.

        Since Mockito 2.1.0, only allow non-null Set. As this is a nullable reference, the suggested API to match null wrapper would be isNull(). We felt this change would make test harnesses much safer than they were with Mockito 1.x.

        See examples in javadoc for ArgumentMatchers class.

        Parameters:
        clazz - Type owned by the Set to avoid casting
        Returns:
        empty Set
        See Also:
        anySet(), isNull(), isNull(Class)
      • anyMap

        public static <K,​V> java.util.Map<K,​V> anyMap()
        Any non-null Map.

        Since Mockito 2.1.0, only allow non-null Map. As this is a nullable reference, the suggested API to match null wrapper would be isNull(). We felt this change would make test harnesses much safer than they were with Mockito 1.x.

        See examples in javadoc for ArgumentMatchers class.

        Returns:
        empty Map.
        See Also:
        anyMapOf(Class, Class), isNull(), isNull(Class)
      • anyMapOf

        @Deprecated
        public static <K,​V> java.util.Map<K,​V> anyMapOf​(java.lang.Class<K> keyClazz,
                                                                    java.lang.Class<V> valueClazz)
        Deprecated.
        With Java 8 this method will be removed in Mockito 4.0. This method is only used for generic friendliness to avoid casting, this is not anymore needed in Java 8.
        Any non-null Map.

        Generic friendly alias to anyMap(). It's an alternative to @SuppressWarnings("unchecked") to keep code clean of compiler warnings.

        This method doesn't do type checks of the map content with the given type parameter, it is only there to avoid casting in the code.

        Since Mockito 2.1.0, only allow non-null Map. As this is a nullable reference, the suggested API to match null wrapper would be isNull(). We felt this change would make test harnesses much safer than they were with Mockito 1.x.

        See examples in javadoc for ArgumentMatchers class.

        Parameters:
        keyClazz - Type of the map key to avoid casting
        valueClazz - Type of the value to avoid casting
        Returns:
        empty Map.
        See Also:
        anyMap(), isNull(), isNull(Class)
      • anyCollection

        public static <T> java.util.Collection<T> anyCollection()
        Any non-null Collection.

        Since Mockito 2.1.0, only allow non-null Collection. As this is a nullable reference, the suggested API to match null would be isNull(). We felt this change would make test harnesses much safer than they were with Mockito 1.x.

        See examples in javadoc for ArgumentMatchers class.

        Returns:
        empty Collection.
        See Also:
        anyCollectionOf(Class), isNull(), isNull(Class)
      • anyCollectionOf

        @Deprecated
        public static <T> java.util.Collection<T> anyCollectionOf​(java.lang.Class<T> clazz)
        Deprecated.
        With Java 8 this method will be removed in Mockito 4.0. This method is only used for generic friendliness to avoid casting, this is not anymore needed in Java 8.
        Any non-null Collection.

        Generic friendly alias to anyCollection(). It's an alternative to @SuppressWarnings("unchecked") to keep code clean of compiler warnings.

        This method doesn't do type checks of the collection content with the given type parameter, it is only there to avoid casting in the code.

        Since Mockito 2.1.0, only allow non-null Collection. As this is a nullable reference, the suggested API to match null would be isNull(). We felt this change would make test harnesses much safer than they were with Mockito 1.x.

        See examples in javadoc for ArgumentMatchers class.

        Parameters:
        clazz - Type owned by the collection to avoid casting
        Returns:
        empty Collection.
        See Also:
        anyCollection(), isNull(), isNull(Class)
      • anyIterable

        public static <T> java.lang.Iterable<T> anyIterable()
        Any non-null Iterable.

        Since Mockito 2.1.0, only allow non-null Iterable. As this is a nullable reference, the suggested API to match null would be isNull(). We felt this change would make test harnesses much safer than they were with Mockito 1.x.

        See examples in javadoc for ArgumentMatchers class.

        Returns:
        empty Iterable.
        Since:
        2.1.0
        See Also:
        anyIterableOf(Class), isNull(), isNull(Class)
      • anyIterableOf

        @Deprecated
        public static <T> java.lang.Iterable<T> anyIterableOf​(java.lang.Class<T> clazz)
        Deprecated.
        With Java 8 this method will be removed in Mockito 4.0. This method is only used for generic friendliness to avoid casting, this is not anymore needed in Java 8.
        Any non-null Iterable.

        Generic friendly alias to anyIterable(). It's an alternative to @SuppressWarnings("unchecked") to keep code clean of compiler warnings.

        This method doesn't do type checks of the iterable content with the given type parameter, it is only there to avoid casting in the code.

        Since Mockito 2.1.0, only allow non-null String. As strings are nullable reference, the suggested API to match null wrapper would be isNull(). We felt this change would make test harnesses much safer than they were with Mockito 1.x.

        See examples in javadoc for ArgumentMatchers class.

        Parameters:
        clazz - Type owned by the collection to avoid casting
        Returns:
        empty Iterable.
        Since:
        2.1.0
        See Also:
        anyIterable(), isNull(), isNull(Class)
      • eq

        public static boolean eq​(boolean value)
        boolean argument that is equal to the given value.

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        value - the given value.
        Returns:
        0.
      • eq

        public static byte eq​(byte value)
        byte argument that is equal to the given value.

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        value - the given value.
        Returns:
        0.
      • eq

        public static char eq​(char value)
        char argument that is equal to the given value.

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        value - the given value.
        Returns:
        0.
      • eq

        public static double eq​(double value)
        double argument that is equal to the given value.

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        value - the given value.
        Returns:
        0.
      • eq

        public static float eq​(float value)
        float argument that is equal to the given value.

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        value - the given value.
        Returns:
        0.
      • eq

        public static int eq​(int value)
        int argument that is equal to the given value.

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        value - the given value.
        Returns:
        0.
      • eq

        public static long eq​(long value)
        long argument that is equal to the given value.

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        value - the given value.
        Returns:
        0.
      • eq

        public static short eq​(short value)
        short argument that is equal to the given value.

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        value - the given value.
        Returns:
        0.
      • eq

        public static <T> T eq​(T value)
        Object argument that is equal to the given value.

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        value - the given value.
        Returns:
        null.
      • refEq

        public static <T> T refEq​(T value,
                                  java.lang.String... excludeFields)
        Object argument that is reflection-equal to the given value with support for excluding selected fields from a class.

        This matcher can be used when equals() is not implemented on compared objects. Matcher uses java reflection API to compare fields of wanted and actual object.

        Works similarly to EqualsBuilder.reflectionEquals(this, other, excludeFields) from apache commons library.

        Warning The equality check is shallow!

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        value - the given value.
        excludeFields - fields to exclude, if field does not exist it is ignored.
        Returns:
        null.
      • same

        public static <T> T same​(T value)
        Object argument that is the same as the given value.

        See examples in javadoc for ArgumentMatchers class

        Type Parameters:
        T - the type of the object, it is passed through to prevent casts.
        Parameters:
        value - the given value.
        Returns:
        null.
      • isNull

        @Deprecated
        public static <T> T isNull​(java.lang.Class<T> clazz)
        Deprecated.
        With Java 8 this method will be removed in Mockito 4.0. This method is only used for generic friendliness to avoid casting, this is not anymore needed in Java 8.
        null argument.

        The class argument is provided to avoid casting.

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        clazz - Type to avoid casting
        Returns:
        null.
        See Also:
        isNull(), isNotNull(), isNotNull(Class)
      • notNull

        public static <T> T notNull()
        Not null argument.

        Alias to isNotNull()

        See examples in javadoc for ArgumentMatchers class

        Returns:
        null.
      • notNull

        @Deprecated
        public static <T> T notNull​(java.lang.Class<T> clazz)
        Deprecated.
        With Java 8 this method will be removed in Mockito 4.0. This method is only used for generic friendliness to avoid casting, this is not anymore needed in Java 8.
        Not null argument, not necessary of the given class.

        The class argument is provided to avoid casting. Alias to isNotNull(Class)

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        clazz - Type to avoid casting
        Returns:
        null.
        See Also:
        isNotNull(), isNull(), isNull(Class)
      • isNotNull

        @Deprecated
        public static <T> T isNotNull​(java.lang.Class<T> clazz)
        Deprecated.
        With Java 8 this method will be removed in Mockito 4.0. This method is only used for generic friendliness to avoid casting, this is not anymore needed in Java 8.
        Not null argument, not necessary of the given class.

        The class argument is provided to avoid casting. Alias to notNull(Class)

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        clazz - Type to avoid casting
        Returns:
        null.
      • nullable

        public static <T> T nullable​(java.lang.Class<T> clazz)
        Argument that is either null or of the given type.

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        clazz - Type to avoid casting
        Returns:
        null.
      • contains

        public static java.lang.String contains​(java.lang.String substring)
        String argument that contains the given substring.

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        substring - the substring.
        Returns:
        empty String ("").
      • matches

        public static java.lang.String matches​(java.lang.String regex)
        String argument that matches the given regular expression.

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        regex - the regular expression.
        Returns:
        empty String ("").
        See Also:
        AdditionalMatchers.not(boolean)
      • matches

        public static java.lang.String matches​(java.util.regex.Pattern pattern)
        Pattern argument that matches the given regular expression.

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        pattern - the regular expression pattern.
        Returns:
        empty String ("").
        See Also:
        AdditionalMatchers.not(boolean)
      • endsWith

        public static java.lang.String endsWith​(java.lang.String suffix)
        String argument that ends with the given suffix.

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        suffix - the suffix.
        Returns:
        empty String ("").
      • startsWith

        public static java.lang.String startsWith​(java.lang.String prefix)
        String argument that starts with the given prefix.

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        prefix - the prefix.
        Returns:
        empty String ("").
      • argThat

        public static <T> T argThat​(ArgumentMatcher<T> matcher)
        Allows creating custom argument matchers.

        This API has changed in 2.1.0, please read ArgumentMatcher for rationale and migration guide. NullPointerException auto-unboxing caveat is described below.

        It is important to understand the use cases and available options for dealing with non-trivial arguments before implementing custom argument matchers. This way, you can select the best possible approach for given scenario and produce highest quality test (clean and maintainable). Please read the documentation for ArgumentMatcher to learn about approaches and see the examples.

        NullPointerException auto-unboxing caveat. In rare cases when matching primitive parameter types you *must* use relevant intThat(), floatThat(), etc. method. This way you will avoid NullPointerException during auto-unboxing. Due to how java works we don't really have a clean way of detecting this scenario and protecting the user from this problem. Hopefully, the javadoc describes the problem and solution well. If you have an idea how to fix the problem, let us know via the mailing list or the issue tracker.

        See examples in javadoc for ArgumentMatcher class

        Parameters:
        matcher - decides whether argument matches
        Returns:
        null.
      • charThat

        public static char charThat​(ArgumentMatcher<java.lang.Character> matcher)
        Allows creating custom char argument matchers. Note that argThat(org.mockito.ArgumentMatcher<T>) will not work with primitive char matchers due to NullPointerException auto-unboxing caveat.

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        matcher - decides whether argument matches
        Returns:
        0.
      • booleanThat

        public static boolean booleanThat​(ArgumentMatcher<java.lang.Boolean> matcher)
        Allows creating custom boolean argument matchers. Note that argThat(org.mockito.ArgumentMatcher<T>) will not work with primitive boolean matchers due to NullPointerException auto-unboxing caveat.

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        matcher - decides whether argument matches
        Returns:
        false.
      • byteThat

        public static byte byteThat​(ArgumentMatcher<java.lang.Byte> matcher)
        Allows creating custom byte argument matchers. Note that argThat(org.mockito.ArgumentMatcher<T>) will not work with primitive byte matchers due to NullPointerException auto-unboxing caveat.

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        matcher - decides whether argument matches
        Returns:
        0.
      • shortThat

        public static short shortThat​(ArgumentMatcher<java.lang.Short> matcher)
        Allows creating custom short argument matchers. Note that argThat(org.mockito.ArgumentMatcher<T>) will not work with primitive short matchers due to NullPointerException auto-unboxing caveat.

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        matcher - decides whether argument matches
        Returns:
        0.
      • intThat

        public static int intThat​(ArgumentMatcher<java.lang.Integer> matcher)
        Allows creating custom int argument matchers. Note that argThat(org.mockito.ArgumentMatcher<T>) will not work with primitive int matchers due to NullPointerException auto-unboxing caveat.

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        matcher - decides whether argument matches
        Returns:
        0.
      • longThat

        public static long longThat​(ArgumentMatcher<java.lang.Long> matcher)
        Allows creating custom long argument matchers. Note that argThat(org.mockito.ArgumentMatcher<T>) will not work with primitive long matchers due to NullPointerException auto-unboxing caveat.

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        matcher - decides whether argument matches
        Returns:
        0.
      • floatThat

        public static float floatThat​(ArgumentMatcher<java.lang.Float> matcher)
        Allows creating custom float argument matchers. Note that argThat(org.mockito.ArgumentMatcher<T>) will not work with primitive float matchers due to NullPointerException auto-unboxing caveat.

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        matcher - decides whether argument matches
        Returns:
        0.
      • doubleThat

        public static double doubleThat​(ArgumentMatcher<java.lang.Double> matcher)
        Allows creating custom double argument matchers. Note that argThat(org.mockito.ArgumentMatcher<T>) will not work with primitive double matchers due to NullPointerException auto-unboxing caveat.

        See examples in javadoc for ArgumentMatchers class

        Parameters:
        matcher - decides whether argument matches
        Returns:
        0.
      • reportMatcher

        private static void reportMatcher​(ArgumentMatcher<?> matcher)