Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito preferrable over EasyMock? [closed]

Tags:

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.

like image 282
manuel aldana Avatar asked Jun 27 '10 13:06

manuel aldana


People also ask

Is Mockito better than EasyMock?

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.

Is EasyMock a mocking framework?

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.

Which is better JMockit or Mockito?

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.

What is the difference between PowerMock and Mockito?

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.


2 Answers

Mockito was developed to allow BDD-style unit testing, that is:

  • Given (the context in which your unit-test runs)
  • When (the events producing the behaviour you're interested in)
  • Then (the outcome you're looking for).

as opposed to

  • Given
  • Expect (here's where the verification gets done)
  • When
  • Then (go back and look at what you wrote in the Expect because there's no actual info here).

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.

like image 134
Lunivore Avatar answered Oct 06 '22 04:10

Lunivore


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:

  • Explicit separation of stub and verification
  • Matchers are based on Hamcrest (also supported by JUnit) instead of a custom API
  • Created mocks are always 'nice'; that is, method calls that are unmocked return clean data (like an empty list) instead of failing

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!

like image 36
mlschechter Avatar answered Oct 06 '22 04:10

mlschechter