I recently blogged about getting started with EasyMock - a simple framework for taking the pain out of creating and using mock objects when unit testing your java code.

In the example I showed that we were expecting the next() method to be called and we wanted the mock ResultSet to return false:

expect(mockResultSet.next()).andReturn(false);

What I didn't explain was that you use the expect() method when you are expecting the mock to return a value. If the method doesn't return a value (such as ResultSet.close()) then there is no need to wrap it in an expect() method call:

mockResultSet.close();

Remember: any methods that you call on your mock prior to the replay() method call are setting up it's expectations not actually calling the methods. After the replay() method is called the mock is in 'live' mode, recording activity and returning any predefined values when it's methods are called.

So here is the same example with the additional expected close() method call:

1 comment:

yes this is exactly what I neededmy test needed to expect a non-void return call from one methodand a void return call from a second methodof the same createMock Objectthus requiring a replay on the createMock Objectso using expectLastCall would not work in this circumstancehowever and expect on the non-void method call followedby just the void method callbefore the replyworked as required