gpt4 book ai didi

java - 检查 EasyMock 模拟是否处于重播模式/状态

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:21:52 26 4
gpt4 key购买 nike

是否可以确定 EasyMock 模拟是否处于重播模式?

类似于:

if (EasyMock.isReplayed(mock))
// do something

最佳答案

为了检查模拟的状态,您需要取消代理您的模拟并检查为该模拟设置的状态,一个状态是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

ClassExtensionHelper.getControl() javadoc

关于java - 检查 EasyMock 模拟是否处于重播模式/状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11267647/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com