Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock a method call with an integer argument greater then X

How can I mock a method call with Mockito with an integer argument value greater then X?

I would like to write something like this:

doReturn("FooBar").when(persons).getPersons(Mockito.gt(10));
like image 623
Oliver Avatar asked Nov 30 '22 00:11

Oliver


1 Answers

Mockito uses the matchers of Hamcrest. All of Mockitos argument matchers use these matchers to match the provided argument in a handy and type-safe way.

Mockito provides also the method argThat(Matcher) to use any matcher implementation of Hamcrest or custom Matcher implementation. There are also specialised versions as intThat(Matcher) for all primitved types.

So, knowing that, I rewrote the mocking of the method call:

doReturn("FooBar")
   .when(persons)
   .getPersons(Mockito.intThat(Matchers.greaterThan(10));
like image 69
Oliver Avatar answered Dec 05 '22 12:12

Oliver