views:

372

answers:

3

Hi there,

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.

+2  A: 

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!

mlschechter
+3  A: 

Mockito may be better now than it was when I last tried it, but it lost me when it changed its API to be incompatible with previous versions. Upgrading to the latest version would have required me to change many of my existing unit tests, which I found unacceptable. I decided it was too immature and unstable for my needs.

That doesn't mean that there's anything wrong with it, though. The version I was using still works fine, although I've since switched back to EasyMock.

CaptainAwesomePants
I can image this was painful. In my case I was starting off with version 1.8.3. Looking at the release-notes the API seems to have stabilized.
manuel aldana
+2  A: 

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.

Lunivore
that's not true, you don't have to 'set up expectations for every interaction'. With Easymock you can just setup a NiceMock (createNiceMock()). Anyway I think that testing object interaction is a good idea (and should be the default/common behaviour).. I seldom use niceMocks
al nik
Mockito was originally a fork of EasyMock, before NiceMock existed.In BDD, they're not tests - just descriptions of behaviour with some examples of how to use a class. The idea of BDD is to make it easy and safe to change, rather than pinning the code down so it doesn't break. Tests are a nice by-product. In that world, testing every interaction doesn't make as much sense as producing readable, easy-to-change examples.
Lunivore
I want to test my code.. then if it's readable and easy to change it is another story.. I don't think TDD Experts would confirm that 'interaction doesn't make much sense'. You always produce something from the interaction of components... I rather prefer to have something that is less readable but test the correct interactions .. IMHO interaction (as readable / easy-to-change code) is a main subject in Testing and shouldn't be avoided so easily
al nik
I don't think TDD experts are necessarily BDD experts. I also never said "interaction doesn't make much sense" - please read the wording I actually used and consider the context.
Lunivore
al nik
If I had already written the BDD book with Dan North, would you trust me more? ;)Here's a BDD example. We have a controller which validates before submitting to the repository. One example covers validation. The other covers saving through the repository. We have two examples which show these two aspects of behaviour. With Mockito, we didn't need to verify validation for the example that showed we saved the item, which helped devs who were new to the code understand what it did. Does that make sense?We only write tests to make things safe to change - otherwise we could test manually.
Lunivore
I really like the definition of http://xunitpatterns.com/Test%20Double.html, which makes the test-aspect very clear. I use Mockito for Test-Stubs, Test-Mocks and Test-Spy. I go the middle/pragmatic way writing tests, they should be easy to write but also should test "something" (to avoid 'false negative' test results). Sure easymock (when not using nice-mocks) is more strict and theoratically more correct but it created a lot of confusing test-cases. in my view inside test-cases the verify step should be the last and not be mixed with the setup (like 'expect' in easymock).
manuel aldana
I like the definition, but (because BDD is partly based on NLP) I prefer to call it a Mock: An imitation, usually with the connotation that it's one of lesser quality (wiktionary.org). I call them this without worrying too much about whether they're setting up context, verifying outcomes, hand-coded or using a framework, because it's an applicable English word, and I like English. Business people are occasionally freaked by the word "test" or the idea that they might have to provide one. They understand "example" or "scenario", though, and have no problem providing them.
Lunivore