Mockito:模拟将在 for 循环中循环的数组列表
2022-09-01 19:50:44
						我有一个正在测试的方法,其中包含以下代码段:
private void buildChainCode(List<TracedPath> lines){
    for(TracedPath path : lines){
        /.../
    }
}
我的单元测试代码如下所示:
public class ChainCodeUnitTest extends TestCase {
    private @Mock List<TracedPath> listOfPaths;
    private @Mock TracedPath tracedPath;
    protected void setUp() throws Exception {
        super.setUp();
        MockitoAnnotations.initMocks(this);
    }
    public void testGetCode() {
        when(listOfPaths.get(anyInt())).thenReturn(tracedPath);
        ChainCode cc = new ChainCode();
        cc.getCode(listOfPaths);
        /.../
    }
}
问题是,在运行测试时,测试代码永远不会进入 for 循环。我应该在什么时间指定条件,以便输入 for 循环?目前我已经指定了,但我想它从未被使用过。when(listOfPaths.get(anyInt())).thenReturn(tracedPath)