Home » » What is the difference between PowerMock, EasyMock and Mockito frameworks ?

What is the difference between PowerMock, EasyMock and Mockito frameworks ?

I am very new to mocking framework, and my work needs mocking framework to finish unit testing. When I try to use mock methods, variables of a class using junit which one of the above frameworks should I prefer? 

------Answer--------

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).
PowerMock is an extension of other Mocking frameworks like Mockito or EasyMock that comes with more powerful capabilities. It means that you can combine Mockito/EasyMock and PowerMock into the same unit test.
I personally use Mockito for unit testing big part of my code and PowerMock only in code that needs its extra features as testing static methods.

 

0 nhận xét:

Post a Comment

Popular Posts

Powered by Blogger.