How to ignore unexpected method calls in JUnit/easymock?

Rory picture Rory · May 15, 2012 · Viewed 11.4k times · Source

I'm just wondering if it is possible using Junit and easymock to ignore unexpected method calls?

I.e. instead of the test failing I want to be able to say - "at this point - ignore any unexpected method calls and just continue with the test as normal'

Thanks

Answer

mmccomb picture mmccomb · May 15, 2012

With EasyMock you can create a nice mock, which unlike a normal mock object does not throw assertion errors if an unexpected/recorded call occurs. To quote the easymock documentation...

On a Mock Object returned by createMock() the default behavior for all methods is to throw an AssertionError for all unexpected method calls. If you would like a "nice" Mock Object that by default allows all method calls and returns appropriate empty values (0, null or false), use createNiceMock() instead.

To create a nice mock, use the static createNiceMock(Class class) method on the Easymock class...

SomeClass someClassNiceMock = EasyMock.createNiceMock(SomeClass.class);

Reference: http://easymock.org/user-guide.html#mocking-nice