是否可以确定 EasyMock 模拟是否处于重放模式?
就像是:
if (EasyMock.isReplayed(mock))
// do something
为了检查state
一个模拟,您需要取消代理您的模拟并检查该模拟的状态集,其中一个状态是ReplayState
. 由于 EasyMock 与 Java 代理一起工作,这很容易:
EasyMock.replay(mock); // setting replay state to a mock object
// stripping proxy and getting the invocation handler
InvocationHandler invocationHandler = Proxy.getInvocationHandler(mock);
// for easyMock, invocation handler holds the state of the mock
ObjectMethodsFilter objectMethodsFilter = (ObjectMethodsFilter) invocationHandler;
// not the not so elegant part:
// this: objectMethodsFilter.getDelegate().getControl().getState()
// retrieves the state instance that can be checked if it is an
// instance of ReplayState.class
boolean inReplayState = objectMethodsFilter.getDelegate()
.getControl().getState() instanceof ReplayState;
就是这样!这将打印true
,因为已设置为Replay
也许对于 3.1 版,您可以使用:
ClassExtensionHelper.getControl(mock).getState() instanceof ReplayState