0

What does the following statement do :

  // The question is about the arguments being passed in the function.
 SomeReturnOutput = CallSomeFunction(with(any(Long.class)), with(any(List.class)));

I tried searching for it, but could not find a satisfying answer. What does with(any(Long.class)) and with(any(List.class)) return ?

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
Kartavya Ramnani
  • 800
  • 9
  • 17

1 Answers1

6

The one "usage" of any() I know about - it is something that mocking libraries like Mockito make use of. See here for example.

To be precise: when you have a mocked object and you specify "expected" calls, then you would be doing something like:

when(someMock.someMethod(any())).thenReturn(whatever);

This basically tells the framework: any object passed should "match". In contrast to:

when(someMock.someMethod(someSpecificValue)).thenReturn(whatever);

that means: only when someMethod() is invoked with that specific value, whatever should be returned.

any(SomeClass.class) is basically a "legacy" version - explicitly stating the expected class. See here for example for further documentation.

No idea about with() though.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Okay, This seems correct. Now that I know its Mockito, I can go through the Documentation and check it out. Thank you. – Kartavya Ramnani Aug 31 '17 at 09:24
  • 1
    Just to be sure: I added another link to the corresponding documentation. But beyond that: when your code is **really** using with(any()) - then maybe this is not about mocking. But then you have to update the question and include the **import** statements at least. – GhostCat Aug 31 '17 at 09:26
  • `with()` may just be a no-op to make the code even more fluently readable I guess? – QBrute Aug 31 '17 at 10:25
  • @QBrute Could be. But as said - I haven't seen that. And there is no with() method; neither in org.mockito.Mockito nor in the ArgumentsMatcher class. – GhostCat Aug 31 '17 at 10:30