I am working on a project where we are currently doing testing with JUnit and Mockito. Now I am trying to find out what would be the side effects of adding PowerMock to the mix.
What are its disadvantages, any dependencies I should know about, any stability issues?
I saw it supports Java 8. Are there any issues there? From what I've read, getting Java 7 support was quite a long road.
EDIT: I guess a questions that would sum it all would be:
What would be the reasons to not use PowerMock?
Power Mock gives you access to mock static methods, constructors etc. and this means that your code is not following best programming principles. Power Mock should be used in legacy applications where you cannot change the code which has been given to you.
In general, I would say yes, we should avoid usage of Powermock. One doubtful case I see for it - you have no time for appropriate design of you code to make it testable enough without power-mockito (, but do you really need that quality of testing, if you don't have time for code-design?)
The main aim of PowerMock is to extend the existing APIs with some methods and annotations to provide extra features that make unit testing quite easy. The PowerMock framework provides a class called PowerMockito used to create mock objects and initiates verification and expectation.
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.
Generally if you start new project and you (want to/ are forced) to use PowerMock because of the architecture of your code it means that this architecture is bad and needs improvement. Power Mock gives you access to mock static methods, constructors etc. and this means that your code is not following best programming principles.
Power Mock should be used in legacy applications where you cannot change the code which has been given to you. Often such code does not have unit/integration tests and even small change can result in bugs in application.
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