Recently I made the switch to Mockito framework and am very happy with it (see also blog-post). The switch from EasyMock to Mockito was very straightforward and I managed to make the tests down compatible (i.e. test cases behave the same).
Do you see real reasons or shootout criteria to prefer EasyMock over Mockito? So far of the codebase I worked with I can't, but am interested in your point of view.
Mockito is the most popular mocking framework used for testing Java applications. It is better than any other testing and mocking framework, such as EasyMock.
EasyMock is a mocking framework, JAVA-based library that is used for effective unit testing of JAVA applications. EasyMock is used to mock interfaces so that a dummy functionality can be added to a mock interface that can be used in unit testing.
JMockit will be the chosen option for its fixed-always-the-same structure. Mockito is more or less THE most known so that the community will be bigger. Having to call replay every time you want to use a mock is a clear no-go, so we'll put a minus one for EasyMock. Consistency/simplicity is also important for me.
While Mockito can help with test case writing, there are certain things it cannot do viz:. mocking or testing private, final or static methods. That is where, PowerMockito comes to the rescue. PowerMockito is capable of testing private, final or static methods as it makes use of Java Reflection API.
Mockito was developed to allow BDD-style unit testing, that is:
as opposed to
IMHO it produces more readable tests, and allows you to separate things like the context in which you're running (setting up the Mocks) and verification of the behaviour you're interested in. Previous mocking frameworks required you to set up expectations for every interaction, regardless of whether it was relevant to the aspect of behaviour you were looking at in that test or not.
I'm more familiar with EasyMock than Mockito, so I had to do a little digging. Mockito has a page that does an explicit comparison from the Mockito point of view.
As I see it, the advantages of Mockito are:
EasyMock has a very similar function set. The core differentiators for Mockito are based on those areas of EasyMock that the Mockito team thought were limitations or sub-optimal practices.
From a functional point of view, neither product is able to mock static methods (I needed to do this for testing without an MBeanServer), but in that case you can use PowerMock on top of either framework.
I'd say go with whichever style fits your testing requirements.
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With