Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expecting anything as parameter to mock using EasyMock

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?

like image 228
Bjarke Freund-Hansen Avatar asked Aug 29 '11 08:08

Bjarke Freund-Hansen


People also ask

How do you handle exceptions in EasyMock?

Here's how I handle exceptions in unit tests: If you aren't explicitly testing for the exception then you should add a throws clause to the method - if the exception was thrown and you weren't expecting it to be thrown then that's a test fail.

What does EasyMock verify do?

EasyMock can ensure whether a mock is being used or not. It is done using the verify() method.


1 Answers

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)); 
like image 86
Péter Török Avatar answered Oct 14 '22 09:10

Péter Török