【发布时间】:2012-06-29 19:12:25
【问题描述】:
是否可以确定 EasyMock 模拟是否处于重放模式?
类似:
if (EasyMock.isReplayed(mock))
// do something
【问题讨论】:
是否可以确定 EasyMock 模拟是否处于重放模式?
类似:
if (EasyMock.isReplayed(mock))
// do something
【问题讨论】:
为了检查模拟的state,您需要unProxy 模拟并检查该模拟的状态集,其中一个状态是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
【讨论】:
replay(mock) 方法是否被调用过。
objectMethodsFilter.getDelegate().getControl()