views:

474

answers:

1

I'm getting frustrated trying to do a simple thing - I want to invoke a method on a mock object and NOT check its return value. I just want to check that it was invoked with the correct parameters.

Example:

MyInterface mockObject = createMock(MyInterface.class);
SomeObject param = new SomeObject();

/* the write object is not void and returns an instance of FooOjbect.
 * I want to ignore everything to do with FooObject - I do not care what
 * it is because I do not store its value. How do I do this? */
mockObject.write(param);

replay(mockObject);

someOtherObjectThatCallsAboveMockObject.process(mockObject);

verify(mockObject);

So are there any EasyMock experts out there? I'm not concerned about the design of the underlying method that I'm calling and not storing the return value because the actually implementation is coming from a third-party networking library (Apache Mina) and I have no control over the API.

EDIT: Conclusion reached some time later

I dumped EasyMock because it wasn't easy and went for Mockito.

+3  A: 

Instead of

mockObject.write(param)

write

EasyMock.expect( mockObject.write(param) ).andReturn( /* return value here */ );

You still need to return a value for the code to be correct but you can ignore the value further on in the test.

Dan
Can you provide an example of how I ignore the value later on? I have scoured the documentation and I don't see any good examples.
Elijah
@Elijah By "ignoring the value" it means your actual class does not depend on the value coming out of it. If you're actual class is not acting on this return value, it means it's ignoring it. You have to provide a return value, period.
Cem Catikkas
... ignore in your test - never reference again. Are you having a problem with your real code? Does andReturn(null) cause problems?You're just trying to make the return value enough so that the code runs the way you desire it to.
Stephen
I misunderstood the meaning of "expect." I was assuming that it was expecting a return value of a certain instance and checking against it. But, it worked as you suggested. Thanks! +1
Elijah