- PowerMock - Home
- PowerMock - Overview
- PowerMock - Environment Setup
- PowerMock - First Application
- PowerMock - Mock Static
- PowerMock - Mock Private
- PowerMock - Mock Final
- PowerMock - Mock New
- PowerMock - Bypass Encapsulation
- PowerMock - Supress Behavior
- PowerMock Useful Resources
- PowerMock - Quick Guide
- PowerMock - Useful Resources
- PowerMock - Discussion
PowerMock - Overview
PowerMock is an extension to Mockito/EasyMock mocking frameworks. It is a JAVA-based library that is used for effective unit testing of JAVA applications. PowerMock is used to mock untestable code such as private/final methods so that complete code can be used in unit testing.
Sometime the programs are not testable or are difficult to test due to multiple problems. Following are the examples where PowerMock comes to rescue.
3rd Party Program/Framework − Some of the framework requires communication via static methods.PowerMock methods allows to setup expectations for these static methods and simulate the behavior which can test. Without PowerMock, we've to create a wrapper across the framework classes and then mock the behavior which could be a tedious exercise.
Private Methods − Private methods cannot be tested directly. PowerMock allows mocking of private and final methods.
Constructors − Some framework requires to subclass their classes containing static initializers (e.g. Eclipse) or constructors (e.g. Wicket). PowerMock allows to remove static initializers and suppress constructors.
Features
Using PowerMock, we can achieve the following −
Mock static methods
Remove static initializers
Allow mocking without dependency injection
Suppress Constructors
PowerMock modifies the byte-code at run-time during the execution cycle of the tests using the reflection API. PowerMock also provides utilities to access the internal state of an object.