Mockito: mocking an arraylist that will be looped in a for loop

后端 未结 2 1592
眼角桃花
眼角桃花 2021-01-31 10:07

I have a method under test that contains the following snippet:

private void buildChainCode(List lines){
    for(TracedPath path : lines){
             


        
2条回答
  •  心在旅途
    2021-01-31 10:37

    Your problem is that when you use a collection in a for-each loop, its iterator() method gets called; and you haven't stubbed that particular method.

    Instead of mocking the list, I strongly recommend you just pass a real list, where the elements are just your mocked TracedPath, as many times as you want it. Something like

    listOfPaths = Arrays.asList(mockTracedPath, mockTracedPath, mockTracedPath);
    

提交回复
热议问题