/* * Copyright (c) 2016 Mockito contributors * This program is made available under the terms of the MIT License. */ package org.mockito; /** * Allows creating customized argument matchers. * This API was changed in Mockito 2.1.0 in an effort to decouple Mockito from Hamcrest * and reduce the risk of version incompatibility. * Migration guide is included close to the bottom of this javadoc. *

* For non-trivial method arguments used in stubbing or verification, you have following options * (in no particular order): *

* *

* Implementations of this interface can be used with {@link Matchers#argThat} method. * Use toString() method for description of the matcher * - it is printed in verification errors. * *


 * class ListOfTwoElements implements ArgumentMatcher<List> {
 *     public boolean matches(List list) {
 *         return list.size() == 2;
 *     }
 *     public String toString() {
 *         //printed in verification errors
 *         return "[list of 2 elements]";
 *     }
 * }
 *
 * List mock = mock(List.class);
 *
 * when(mock.addAll(argThat(new ListOfTwoElements))).thenReturn(true);
 *
 * mock.addAll(Arrays.asList("one", "two"));
 *
 * verify(mock).addAll(argThat(new ListOfTwoElements()));
 * 
* * To keep it readable you can extract method, e.g: * *

 *   verify(mock).addAll(argThat(new ListOfTwoElements()));
 *   //becomes
 *   verify(mock).addAll(listOfTwoElements());
 * 
* * In Java 8 you can treat ArgumentMatcher as a functional interface * and use a lambda, e.g.: * *

 *   verify(mock).addAll(argThat(list -> list.size() == 2));
 * 
* *

* Read more about other matchers in javadoc for {@link Matchers} class. *

2.1.0 migration guide

* * All existing custom implementations of ArgumentMatcher will no longer compile. * All locations where hamcrest matchers are passed to argThat() will no longer compile. * There are 2 approaches to fix the problems: * * What option is right for you? If you don't mind compile dependency to hamcrest * then option b) is probably right for you. * Your choice should not have big impact and is fully reversible - * you can choose different option in future (and refactor the code) * * @param type of argument * @since 2.1.0 */ public interface ArgumentMatcher { /** * Informs if this matcher accepts the given argument. *

* The method should never assert if the argument doesn't match. It * should only return false. *

* See the example in the top level javadoc for {@link ArgumentMatcher} * * @param argument * the argument * @return true if this matcher accepts the given argument. */ boolean matches(T argument); }