rdhaese
10/3/2017 - 10:58 AM

Check if Object is in Replay State (EasyMock)

Utility method to check if an EasyMock mock object is in the replay state.

 /**
     * Checks if a EasyMock mock is in the replay state.
     *
     * @param mock to check replay state for
     * @return true if the mock is in replay state, false if not or if the param
     * is no mock
     */
    public boolean isInReplayState(final Object mock) {
        InvocationHandler invocationHandler = Proxy.getInvocationHandler(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
        return objectMethodsFilter.getDelegate()
                .getControl().getState() instanceof ReplayState;
    }