3

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.

EnzoMolion
  • 949
  • 8
  • 25

2 Answers2

2

try:

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

verify(mObject).verifiedMethod(not(eq(longValueThatShouldBeAvoided)));
Misha Lemko
  • 591
  • 4
  • 9
  • Would you mind to specify which `not` method you suggest please ? I mean, which package it is declared in. – EnzoMolion Feb 19 '20 at 15:27
  • 1
    @EnzoMolion `import static org.mockito.AdditionalMatchers.not; import static org.mockito.ArgumentMatchers.eq;` – Misha Lemko Feb 19 '20 at 15:30
  • 1
    It worked, thanks ! I thought I tried this but my IDE was actually importing `org.hamcrest.Matcher`'s `not`... – EnzoMolion Feb 19 '20 at 15:35
1

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.

  • While this question actually perfectly answers my question and several others do, I did not find them during my research, sorry about that. – EnzoMolion Feb 19 '20 at 15:37