Expecting anything as parameter to mock using EasyMock

Bjarke Freund-Hansen picture Bjarke Freund-Hansen · Aug 29, 2011 · Viewed 34.4k times · Source

Using EasyMock I want to be able to say that I expect a specific method called on my mock, but I do not care about the parameter which are used to call the mock.

SomeInterface mock = EasyMock.createMock(SomeInterface.class);
mock.send(/*anything*/);
replay(mock);

/* Perform actions that will eventually invoke mock */

verify(mock);

Is this possible, and how?

Additionally if I want to accept any object that derives from a specific base class, how do I specify that?

Answer

Péter Török picture Péter Török · Aug 29, 2011

To accept any object as parameter:

mock.send(anyObject());

(You may need to cast the expression to the desired type.)

In addition, to accept any object of a specific type, use:

mock.send(isA(SomeObject.class));