I'd like to verify using Mockito that a string argument satisfies two conditions:
verify(mockClass).doSomething(Matchers.startsWith("prefix")); verify(mockClass).doSomething(Matchers.endsWith("suffix"));
How to combine those two into one statement?
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).
Matcher methods can't be used as return values; there is no way to phrase thenReturn(anyInt()) or thenReturn(any(Foo. class)) in Mockito, for instance. Mockito needs to know exactly which instance to return in stubbing calls, and will not choose an arbitrary return value for you.
mockito. Matchers is deprecated, ArgumentMatchers should be used instead.
This is possible using org.mockito.AdditionalMatchers
:
import static org.mockito.AdditionalMatchers.and; verify(mockClass).doSomething( and(Matchers.startsWith("prefix"), Matchers.endsWith("suffix"));
There are also org.mockito.AdditionalMatchers.or
and org.mockito.AdditionalMatchers.not
.
Prior comments have called out that and
takes only two arguments, and that having a variant that takes three or more would be advantageous. The following code solves this recursively, allowing multiple matchers to be specified in varargs:
public static String conjunction(String... matchers) { if (matchers.length < 2) { throw new IllegalArgumentException("matchers.length must be >= 2"); } else if (matchers.length == 2) { return and(matchers[0], matchers[1]); } else { final String[] tail = new String[matchers.length - 1]; System.arraycopy(matchers, 1, tail, 0, tail.length); return and(matchers[0], conjunction(tail)); } }
assuming the following imports:
import static org.mockito.AdditionalMatchers.*; import static org.mockito.Mockito.*;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With