简体   繁体   English

是否有用于模拟存根的 `not` ArgumentMatcher

[英]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.我试图验证一个方法被调用的long具有除给定值之外的任何值。

I thus would like to know if there is an ArgumentMatcher that fits to my use case, such as:因此,我想知道是否有适合我的用例的ArgumentMatcher ,例如:

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.但我觉得奇怪的是,这么简单的ArgumentMatcher必须从头开始编写。


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.同样,我找到了使用arg -> arg != val0 && arg != val1 lambda 作为ArgumentsMatcher.longThat方法的ArgumentsMatcher.longThat来实现这一点的解决方法。

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 .我认为您可能正在AdditionalMatchers 中寻找“非”匹配器。

However, I think that is dealt with more extensively in this other question .但是,我认为在另一个问题对此进行了更广泛的处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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