Question

I'm learning testing with EasyMock and Mockito. What are differences between them? What are advantages and disadvantages of any of them? Which one is better to use?

Was it helpful?

Solution

Although this question is majorly opinion based but you get the differences from here:

Differences:

  • no record/replay modes - no need for them. There only 2 things you can do with Mockito mocks - verify or stub. Stubbing goes before execution and verification afterwards.
  • all mocks are 'nice' (even somehow nicer, because collection-returning methods return empty collections instead of nulls). Even though mocks are nice, you can verify them as strictly
    as you want and detect any unwanted interaction.
  • explicit language for better readability: verify() and when() VS the mixture of expect(mock.foo()) and mock.foo() (plain method call without 'expect'). I'm sure some of you will find this argument subjective :)
  • simplified stubbing model - stubbed methods replay all the time with stubbed value no matter how many times they are called. Works exactly like EasyMock's andStubReturn(), andStubThrow(). Also, you can stub with different return values for different arguments (like in EasyMock).
  • Verification of stubbed methods is optional because usually it's more important to test if the stubbed value is used correctly rather than where's it come from.
  • verification is explicit - verification errors point at line of code showing what interaction failed. verification in order is flexible and doesn't require to verify every single interaction.
  • custom argument matchers use hamcrest matchers, so you can use your existing hamcrest matchers. (EasyMock can also integrate with hamcrest though it is not a part of EasyMock but hamcrest. See the
    documentation of hamcrest).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top