EasyMock experience
Luca | February 23, 2008For the last two weeks I’ve worked with EasyMock and coming from a JMock background it’s easy to make a comparison between the two libraries.
I have to say that I’m less than impressed by EasyMock: the whole concept of the two different states (recording and active) for the mock library looks unreasonable.
Let’s look on how we can create a mock with EasyMock:
MyInterface mock =EasyMock.mock(MyInterface.class);
//expectation
mock.myMethod();
//activation step
mock.replay();
//actual call to the mock
mock.myMethod();
//verification that the method has been called
mock.verify();
There’s also a more DSLish style of defining expectations, that I personally prefer (it differentiates clearly the definition of the expectation from the actual method call).
EasyMock.expects(mock.myMethod());
This style is the only one available when the expectation is more complex:
EasyMock.expects(mock.myMethod()).andReturn(true);
But what if I want to define that a certain method will be called on the stub, no matter how many times ?
I can either use a different way of creating the mock:
EasyMock.createNiceMock(MyInterface.class);
or using the DSL way:
EasyMock.expects(mock.myMethod()).anyTimes()
I think that having two ways of defining the same expectation pretty confusing, specially when you’re using the API for the first time.
What I’m looking for in a mock library is the possibility of defining the expectations in a concise but expressive way (DSL please…) and then inject the dependency in my main object. Period. Done.
JMock2 is pretty close, but I have to admit the the inner class notation with all those curly brackets around is not helping.
A fellow ThoughtWorker have just released Mockito: it looks like it’s taking the best from EasyMock and JMock and put in a single library..will it be true ? I’ll give it a try and I’ll let you know






Mockito seems very simple and clear but works only with
uberto | February 27, 2008Mockito seems very simple and clear but works only with interfaces.
Maybe this is not a big problem because when I needed to mock a concrete class (because of bad design) I had had to create my own mock class, for the same bad design reasons.
I'm a little bit after the battle since this post
Henri | December 28, 2008I’m a little bit after the battle since this post is quite old. Anyway, I must say I’m a little bit puzzled by your syntax.
A mock is replayed/verified doing replay(mock)/verify(mock).
A stub is created doing expect(mock,someMethod()).andStubReturn(something).
createNiceMock is used to have the mock returning default values by… default. You can then record a real behavior if needed (stub or not).
So I don’t feel you’ve tested EasyMock enough before commenting. However, I must agree on one thing, Mockito really seems to have pull-up some nice ideas.
Hi Henri, thank you for the clarification ! I'll definitively give another
Luca | January 25, 2009Hi Henri,
thank you for the clarification !
I’ll definitively give another try to EasyMock even if the replay/verify style of interaction with the mock doesn’t convince me yet !