EasyMock 中的“.andReturn(...).anyTimes()”和“.andStubReturn(...)”有区别吗?

Is there any difference between ".andReturn(...).anyTimes()" and ".andStubReturn(...)" in EasyMock?

This post 表明两者存在差异(请参阅用户 SnoopyMe 的评论)并且两者可以互换使用。 EasyMock 文档没有提到任何区别。

在实践上或语义上有什么区别吗?如果是这样,什么时候使用一个比另一个更合适?

编辑:

以下测试表明存在差异,至少在与严格模拟一起使用时:

@Test
public void testTestMe() {
    Bar bar = createStrictMock(Bar.class);
    expect(bar.doBar()).andReturn(1).anyTimes();
    expect(bar.doOtherBar()).andReturn(2).once();
    replay(bar);

    Foo foo = new Foo(bar);
    foo.testMe();
    verify(bar);
}

@Test
public void testTestMeAgain() {
    Bar bar = createStrictMock(Bar.class);
    expect(bar.doBar()).andStubReturn(1);
    expect(bar.doOtherBar()).andReturn(2).once();
    replay(bar);

    Foo foo = new Foo(bar);
    foo.testMe();
    verify(bar);
}

public class Foo {
    private final Bar _bar;
    public Foo(Bar bar) {
        _bar = bar;
    }

    public void testMe() {
        _bar.doBar();
        _bar.doOtherBar();
        _bar.doBar();
    }
}

andReturn(...).anyTimes() 仍然会验证顺序,这是由严格的模拟验证强制执行的。然而,andStubReturn(...) 不会。

但是,我仍然不清楚这是唯一的区别,还是语义上的区别。例如,对于常规(非严格)模拟,anyTimes() 是否与 stubReturn() 相同?

结果上的差异很小。对于普通模拟,它是相同的。然而,对于一个严格的模拟,存根可以在任何时候被调用而无需考虑顺序。 anyTimes 可以随时调用,但只能按记录的顺序调用。

这种差异是由语义差异造成的。存根是可以在测试期间随时调用的东西,您并不真正关心何时以及调用多少次。事实上,大多数模拟方法通常都是存根。

那么anyTimes表示你很在意。但事实上,我从不使用它。当我进行测试时,我知道会发生什么。我的方法总是被存根或被期望被调用准确的时间。