我正在使用 EasyMock 进行一些单元测试,但我不了解EasyMock.expectLastCall()
. 正如您在下面的代码中看到的那样,我有一个对象,该对象的方法返回 void 在其他对象的方法中被调用。我认为我必须让 EasyMock 期望该方法调用,但我尝试注释掉expectLastCall()
调用并且它仍然有效。是因为我通过EasyMock.anyObject())
了它将它注册为预期的呼叫还是发生了其他事情?
MyObject obj = EasyMock.createMock(MyObject.class);
MySomething something = EasyMock.createMock(MySomething.class);
EasyMock.expect(obj.methodThatReturnsSomething()).andReturn(something);
obj.methodThatReturnsVoid(EasyMock.<String>anyObject());
// whether I comment this out or not, it works
EasyMock.expectLastCall();
EasyMock.replay(obj);
// This method calls the obj.methodThatReturnsVoid()
someOtherObject.method(obj);
EasyMock 的 API 文档这样说expectLastCall()
:
Returns the expectation setter for the last expected invocation in the current thread. This method is used for expected invocations on void methods.