Jmockit:无法使用带有集合和多个调用的returns()?

时间:2015-01-27 18:45:28

标签: jmockit

我想模拟的功能:

class Bar {
  public Set<Foo> getFoos();
}

正在测试的代码:

for (int i = 0; i < n; ++i) {
  Bar bar = computeBar();
  for (Foo f : bar.getFoos()) {
    // code
  }
}

期望阻止:

new Expectations() {{
  bar.getFoos();
  returns(/* what should I put here?? */);
}};

1 个答案:

答案 0 :(得分:1)

new Expectations() {{ bar.getFoos(); returns(foo1, foo2, foo3); }};

返回单个集,或

final Set<Foo> foos1 = new HashSet<Foo>(asList(foo1, foo2));
final Set<Foo> foos2 = new HashSet<Foo>(asList(foo3, foo4, foo5));
new Expectations() {{ bar.getFoos(); returns(foos1, foos2); }};

返回一系列集。