简体   繁体   中英

Is there a `not` ArgumentMatcher for mockito stubbing

I am trying to verify that a method is called with a long having any value but a given one.

I thus would like to know if there is an ArgumentMatcher that fits to my use case, such as:

verify(mObject).verifiedMethod(notEq(longValueThatShouldBeAvoided));

I found this workaround:

verify(mObject).method(longThat(arg -> arg != longValueThatShouldBeAvoided));

But I find weird that such simple ArgumentMatcher has to be written from scratch.


Additional question: How to proceed when checking for multiple values to avoid ?

Similarly, I found the workaround of using arg -> arg != val0 && arg != val1 lambda as parameter of ArgumentsMatcher.longThat method to achieve this.

try:

import static org.mockito.AdditionalMatchers.not; 
import static org.mockito.ArgumentMatchers.eq;

verify(mObject).verifiedMethod(not(eq(longValueThatShouldBeAvoided)));

I think you might be looking for the "not" matcher in AdditionalMatchers .

However, I think that is dealt with more extensively in this other question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM