PowerMock
PowerMock is an open-source Java framework used for creating a mock object in unit testing. It extends other mocking frameworks such as EasyMock and Mockito to enhance the capabilities. The PowerMock framework uses a custom classloader and bytecode manipulation techniques to enable the mocking of static methods, final classes, final methods, private methods, constructor, and removal of static initializers. 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. The PowerMockito provides the functionality to work with the Java reflection API.
Let’s understand the concept of PowerMock with the help of an example.
Example of PowerMock with Mockito
Here, we are going to create an example of PowerMock with Mockito and JUnit frameworks. To create an example using PowerMock, we need to go through the following steps.
Step 1: Add the following PowerMock dependencies in pom.xml file.
Step 2: Apply the PowerMock annotations
To use PowerMock with Mockito, we need to apply the following two annotations in the test:
@RunWith(PowerMockRunner.class): It is the same as we have used in our previous examples. The only difference is that in the previous example we have used MockitoUnitRunner.class, now we will use PowerMockRunner.class for enabling the PowerMockito APIs in the test.
@PrepareForTest: It tells PowerMock to prepare some classes for testing. It can be applied to both the test classes and the individual test methods. It includes classes with final, static, private, or native methods that can be mocked.
Note: The above two steps are mandatory for all the examples we are going to create using PowerMock.
There are following examples of PowerMock with Mockito and JUnit frameworks.
1. Mocking static methods
In the following example, we are going to mock static methods.
Step 1: Create a class that contains a static method. We have created a class with the name Utility.
Utility.java
Step 2: Create a JUnit test case, named Powermock_test, for testing purpose.
Powermock_test.java
Output
The following output shows that the test is successfully running.
We can also verify that the static method is called or not, by using the verifyStatic() method of PowerMockito, as shown below.
2. Mocking private methods
In the following example, we will create a mock of private methods.
Step 1: Create a class that contains a private method. We have created class with the name Utility and defined a private method and a public method (that returns the object of private method).
Utility.java
Step 2: Create a JUnit test case named PowerMock_test for testing purpose.
Powermock_test.java
Output
The following output shows that the test is successfully running.
3. Mocking final methods
In the following example, we are going to mock final methods.
Step 1: Create a class that contains a final method. We have created a class with the name Utility and defined a final method named finalMethod.
Utility.java
Step 2: Create a JUnit test case named Powermock_test for testing purposes.
Powermock_test.java
Output
The following output shows that the test is successfully running.