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

keya picture keya · Sep 3, 2016 · Viewed 24.3k times · Source

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 ?

Answer

GhostCat picture GhostCat · Sep 4, 2016

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).