Is there a way to list the items in a list in the mockito thenReturn function so that I return every item in the list. So far I have done this:
List<Foo> returns = new ArrayList<Foo>();
It works exactly the way I want it. Each time a function is called, it returns another object from the list, for example get(1) , get(2) , etc.
But I want to simplify this and make it more dynamic for any list of sizes, if I have a list of size 100. I tried something like this:
Mockito.when( /* some function is called */ ).thenReturn( for(Foo foo : returns) { return foo; } );
I also tried this:
Mockito.when(service.findFinancialInstrumentById(eq(1L))).thenReturn( for (int i=0; i<returns.size(); i++) { returns.get(i); } );
But this will not work ... since I list this list in thenReturn .... I came across other methods like then or answer , but I do not work best in this scenario.
source share