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

后端 未结 2 1587
眼角桃花
眼角桃花 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:36

    The Java For-Each loop used inside your buildChainCode method isn't calling get(), as you already figured out - it's using the iterator() method defined in Collection, which List extends.

    I really wouldn't recommend mocking the list. There's no advantage to it, unless you absolutely must check that the list was iterated through, and even then, it's better to assert on or verify the results of your class's behaviour given certain inputs.

    Pass some implementation of List like LinkedList with tracedPath in it.

提交回复
热议问题