Back

Easier mocking with EasyMock

Added: November 05, 2009

Tags: testing

I was helping my colleague to understand piece of code he needed to modify. Finally he was satisfied with changes and executed newly written test. Green. My task is done, let's go back to my own business. As I was leaving I got an idea. I have asked him something I usually do instead of open endorsing of TDD.

Me: Have you seen the test fail?
He: No.
Me: So how do you know it works? Because it is green? Green does not tell you a test works correctly.

So I grabbed the keyboard again, commented whole tested method out and executed test again. Green. I did not expect I will make my point so clearly.

After some exploration we found that he forgot to replay and verify a new instance of mock. That's the thing I hate about EasyMock. To overcome this problem I usually register all mocks in a collection and always replay and verify all mocks in the collection. And I am used to do verification during tear down. I usually do that in superclass of test.

I wanted to write a piece of code showing how to achieve that and went to EasyMock site to check on some details. And I have found that since version 2.5.2 it has EasyMockSupport that does exactly that! I do not know what took them so long to add it, but I am glad it is part of the library now.

Back