0

I have a method that returns the quantity of rows of something like a log.

So if I call the first time, returns 0 rows. But if I call one more time, it could return one or more rows.

someList = getLogLines("unique-id");

Another method has a logic related to the quantity of rows of the first method.

initialRows = getLogLines("unique-id");
if(initialRows.size()==0){
  //some logic
}
//another lines and logic
newRows = getLogLines("unique-id");
if(newRows.size()>0){
  //another logic
}

As you can see getLogLines("unique-id") is called several times with the same argument "unique-id".

On unit test, How can I mock getLogLines to receive a result according to the number of invocations with the same argument?

If each invocation would have different arguments, it would be easy to mock

doReturn(emptyRows).when(Foo).getLogLines("bar-id");
doReturn(severalRows).when(Foo).getLogLines("baz-id");

But I cannot find how to mock different result when the argument is the same.

I only found Mockito.times(x) but I cannot find how to combine verify and doReturn:

JRichardsz
  • 14,356
  • 6
  • 59
  • 94

0 Answers0