Sotirios Delimanolis Sotirios Delimanolis - 1 year ago 123
Java Question

EasyMock expectations with void methods

I'm using EasyMock to do some unit tests and I don't understand the usage of

. 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
invocation and it still works. Is it because I passed
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);


// whether I comment this out or not, it works


// This method calls the obj.methodThatReturnsVoid()

The API doc for EasyMock says this about

Returns the expectation setter for the last expected invocation in the current thread. This method is used for expected invocations on void methods.

Answer Source

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.


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.