Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expect void method call with any argument using EasyMock

As a part of unit test I need to mock a void function(Which accept any non-primitive paramter. e.g. MAP) call with any argument.

mockObj.myMethod(<anyObject>)

Is it possible to do this with EasyMock?

like image 646
shantanu Avatar asked Jun 15 '13 13:06

shantanu


People also ask

How do you expect a void method to call in EasyMock?

andVoid() 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. You can checkout complete project and more EasyMock examples from our GitHub Repository.

How do you mock a response to a void method?

Mockito provides following methods that can be used to mock void methods. doAnswer() : We can use this to perform some operations when a mocked object method is called that is returning void. doThrow() : We can use doThrow() when we want to stub a void method that throws exception.

What does EasyMock expect do?

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.


1 Answers

Use either of the anyObject methods: anyObject() or anyObject(T)

So

 expect(mockObj.myMethod(anyObject()));

See the Flexible Expectations with Argument Matchers section of the documentation

like image 194
John B Avatar answered Sep 18 '22 07:09

John B