Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore unexpected method calls in JUnit/easymock?

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

like image 606
Rory Avatar asked May 15 '12 10:05

Rory


People also ask

How do you mock a void in EasyMock?

If we just want to mock void method and don't want to perform any logic, we can simply use expectLastCall(). andVoid() right after calling void method on mocked object.

Which of the given method is used to create EasyMock?

The expect() method tells EasyMock to simulate a method with certain arguments. The andReturn() method defines the return value of this method for the specified method parameters. The times() method defines how often the Mock object will be called. The replay() method is called to make the Mock object available.

How does EasyMock work?

easymock. EasyMock: mock(…): generates a mock of the target class, be it a concrete class or an interface. Once created, a mock is in “recording” mode, meaning that EasyMock will record any action the Mock Object takes, and replay them in the “replay” mode.

Which among the following are benefits of using EasyMock?

Benefits of EasyMockNo Handwriting − No need to write mock objects on your own. Refactoring Safe − Renaming interface method names or reordering parameters will not break the test code as Mocks are created at runtime. Return value support − Supports return values. Exception support − Supports exceptions.


1 Answers

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

like image 78
mmccomb Avatar answered Sep 20 '22 01:09

mmccomb