I am very new to mocking framework, and my work needs mocking framework to finish unit testing. In the current code base i could see above 3 frameworks are being used in different places for unit testing. So, which one should i go for in the above 3 frameworks ?
Test Spies Support One of the vital differences between Mockito and EasyMock is that Mockito supports spies and mocks. Whereas EasyMock does not support spies. Spy and Mocks have different functionality.
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.
The division of work between the two is that Mockito is kind of good for all the standard cases while PowerMock is needed for the harder cases. That includes for example mocking static and private methods.
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.
I give you an explanation that other people probably wont like, but I (and a lot of people I mentioned it to) find/found very helpful: PowerMock is the mocking framework ... that you better not use.
The main advantage from PowerMock is that you can use it to test certain constructs (for example static method invocations) that EasyMock can't mock. Thus: when you want to test 3rd party code that you can't change; and that contains static calls; then you might turn to PowerMock.
But when you write your own code, and you focus on writing testable code; then you will find that the "need to use PowerMock" is absolutely equal to "you did a bad job designing your code". Using static for example directly leads to direct coupling between your classes; and therefore it is hard to get rid of it once it is in.
Don't get me wrong - PowerMock has its place in testing; but its powerful features come at certain cost.
On EasyMock / Mockito: mainly "two different ways" of writing down test cases; the later one leading to tests that many people find easier to read; whereas EasyMock and "strict" mocks allow for writing down test cases that will break quickly upon most changes in your production code (which by itself can be very useful, or very annoying).
Here you can find a comparision between Mockito and EasyMock:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With