是否可以确定EasyMock模拟是否处于重放模式?
类似的东西:
if (EasyMock.isReplayed(mock))
// do something
答案 0 :(得分:4)
为了检查模拟的state
,你需要 unProxy 你的模拟并检查该模拟的状态集,一个状态是ReplayState
。由于EasyMock适用于Java Proxies,因此非常简单:
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