I am writing this as finding the solution to the problem that I was having while mocking a Writer in Java using EasyMock took far too long. Hopefully others having the same issue will stumble across this blog post.

I have a class that takes a Writer as a constructor argument. This Writer is then used throughout the class! Code for the constructor can be seen below:

After reading the EasyMock documentation and other pages and tutorials etc. on-line I established that mocks are created using EasyMock.createMock() and then expected method calls are added using EasyMock.expect(). (You can also use the static methods). My first attempt looked something like this:

 

This however will not build. This is due to the fact that EasyMock.expect() can not be used with void methods, of which Writer.write is one. After a little more digging into how mocking in Java even worked and a little more reading I came up with the below solution:

If you call a method on a mock before EasyMock.reply() is called this will be counted as an expected method if Easymock.expectLastCall() is called after each of the expected method calls. So for expecting multiple calls on an object where the method returns void you would end up with some code that looks like this:

This example was created when making this commit.