Comprobar si un simulacro de EasyMock está en modo/estado de reproducción

Is it possible to determine if an EasyMock mock is on replay mode?

Algo como:

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

preguntado el 29 de junio de 12 a las 20:06

1 Respuestas

Para comprobar el state of a mock you need to unProxy your mock and check the state set for that mock, and one state is the ReplayState. As EasyMock works with Java Proxies, this is quite easy:

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;

And thats it! This will print true because was already set to Replay

Maybe for version 3.1 you could use:

ClassExtensionHelper.getControl(mock).getState() instanceof ReplayState

ClassExtensionHelper.getControl() javadoc

Respondido 08 Jul 12, 19:07

What I really want to determine is if the replay(mock) method was called at all. - LandonSchropp

ohhh I got it... let me correct the answer ;) I used it some time ago! - Francisco Spaeth

Thanks for the update. Wow, that seems really complicated. Is there any simpler way to do it? - LandonSchropp

I haven't tested it quite yet. I wanted to make sure there wasn't a way in the existing API. - LandonSchropp

And if you take a look into EasyMock class code you will find it as a private method: private static MocksControl getControl(Object mock) { ObjectMethodsFilter handler = (ObjectMethodsFilter) Proxy .getInvocationHandler(mock); return handler.getDelegate().getControl(); } that is more or less this: objectMethodsFilter.getDelegate().getControl() - Francisco Spaeth

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.