Wednesday, April 15, 2015

TDD - Powermock advantages & Disadvantages

When I was writing unit tests using Easymock, I was surprised how to deal with static methods, classes without interfaces etc... That is because Easymock does not provide the ability of mocking the static methods and class mocking. I started to search for a solution and found, Powermock is an extension to Easymock where it adds the above missing features to Easymock. I was so happy and I was thinking why Easymock is not bundled together with Powermock extension.

After I started to practice Test Driven Development (TDD), I realized why Powermock is not a default extension for Easymock.

Whats wrong with Powermock in TDD?

"TDD drives the design". Loose coupling is one of the most important aspect of a good design. It is known that static methods increases coupling. (Hard/Impossible to use IoC). If static method mocking is allowed, you will start writing static methods. The class mocking discourages the use of interfaces which again increases coupling. Therefore enabling static mocking in a TDD support library drives the programmer away from the goals of TDD.

When to use Powermock in TDD?

Is use of Powermock a sin like a murder? why still PowerMock is around? The answer is "Yes we need static method mocking & class mocking like features in certain cases."

These features are necessary when you practice TDD for a new code that has a legacy code or a library as a dependency. There can be static methods or classes without interfaces which are already written in the old code that will be used within the new code. In this case, Power

Conclusion


Always try to avoid mock libraries which supports static method mocking by default.
Use static mocking supported mock library when you practice TDD for new code you write on top of legacy code.