Mockito preferrable over EasyMock?

manuel aldana picture manuel aldana · Jun 27, 2010 · Viewed 9.8k times · Source

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.

Answer

Lunivore picture Lunivore · Jun 28, 2010

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.