Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EasyMock expectations with void methods

Tags:

I'm using EasyMock to do some unit tests and I don't understand the usage of EasyMock.expectLastCall(). As you can see in my code below, I have an object with a method that returns void getting called in some other object's method. I would think that I have to make EasyMock expect that method call, but I tried commenting out the expectLastCall() invocation and it still works. Is it because I passed EasyMock.anyObject()) that it registered it as an expected call or is there something else going on?

MyObject obj = EasyMock.createMock(MyObject.class); MySomething something = EasyMock.createMock(MySomething.class); EasyMock.expect(obj.methodThatReturnsSomething()).andReturn(something);  obj.methodThatReturnsVoid(EasyMock.<String>anyObject());  // whether I comment this out or not, it works EasyMock.expectLastCall();  EasyMock.replay(obj);  // This method calls the obj.methodThatReturnsVoid() someOtherObject.method(obj); 

The API doc for EasyMock says this about expectLastCall():

Returns the expectation setter for the last expected invocation in the current thread. This method is used for expected invocations on void methods. 
like image 664
Sotirios Delimanolis Avatar asked Dec 17 '12 15:12

Sotirios Delimanolis


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.

What method is used to make 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 do you mock method which returns void?

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.

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.


1 Answers

This method returns you the handle of expectation through IExpectationSetters; which gives you ability to validate(assert) that your void method was called or not and related behaviors e.g.

EasyMock.expectLastCall().once(); EasyMock.expectLastCall().atLeastOnce(); EasyMock.expectLastCall().anyTimes(); 

Detailed API of the IExpectationSetters is here.

In your example you are just getting the handle and not doing anything with it hence you don't see any impact of having or removing the statement. It's very same as you call some getter method or declare some variable and don't use it.

like image 62
Yogendra Singh Avatar answered Sep 17 '22 13:09

Yogendra Singh