EasyMock - 如何重置模拟但保持期望?

Ben*_*Ben 8 testing easymock mocking

是否有可能在同一个模拟对象实例上重新定义特定的期望?

假设我有这个测试验证OK:

List<String> foo = createMock(List.class);
expect(foo.get(1)).andReturn("Wibble").once();
expect(foo.size()).andReturn(1).once();
replay(foo);
System.out.println(foo.get(1));
System.out.println(foo.size());
verify(foo);
Run Code Online (Sandbox Code Playgroud)

我想要做的是重置模拟,保持所有定义的期望,但更改其中一个,说:

reset(foo);
// Redefine just one of the two expectations
expect(foo.get(1)).andReturn("Wobble").once();                
System.out.println(foo.get(1));
System.out.println(foo.size());
verify(foo);
Run Code Online (Sandbox Code Playgroud)

由于在重置调用后未定义foo.size,因此不会在分钟时工作.

必须是一个很好的方式来做到这一点,而不是每次都重建期望?

提前致谢

Doc*_*uss 2

您可以将期望写成函数并将期望的参数作为参数传递吗?这就是我以前做过的事情。

private List<String> setExpectations(String expectedString) {
  List<String> foo = createMock(List.class);
  expect(foo.get(0)).andReturn(expectedString).once();
  expect(foo.size()).andReturn(1).once();
  replay(foo);
  return foo;
}
Run Code Online (Sandbox Code Playgroud)

加:返回第零个字符串,是吗?