PowerMock tutorial

1. Overview

Mockito is a powerful, open-source Mocking framework in Java. The features it provides for unit-testing is inevitably unique and important, nonetheless, ease out a lot of work for developers while writing unit test cases.

While Mockito can help with virtually everything, there are some things it cannot do. Like stubbing or testing private, final or static methods. It needs much more power to write test cases for such methods which usually causes developers to write cumbersome code for these methods.

Here, PowerMockito comes to the rescue. PowerMockito is capable of testing private, final or static methods as it makes use of Java Reflection API. Let’s see the demonstration in action while we study its uses.

2. Create a simple java maven project.

3. Adding Dependencies with Maven

As we have talked earlier, the best way to get started with Mockito, or PowerMockito is to find its Maven dependency and add it to our project. Here are the dependencies we need to add:

Now that we are done adding dependencies, let’s enable the use of annotations in our tests.

Your pom.xml will look like below:

4. Enabling PowerMock Annotations

Just like what we need to do with Mockito, we also need to enable the use of Annotations with PowerMockito. Much like Mockito, we make use of similar annotations, as shown:

Let us look at each annotation we used above:

  • @RunWith annotation is similar to what we did in Mockito. Instead of using Mockito, we will use a PowerMockRunner this time, which will enablePowerMockito APIs in the test.
  • @PrepareForTest was provided a fullyQualifiedNames package with a wildcard. This informs PowerMockito which classes to prepare with Java Reflection API for testing.

5. Mocking final methods

Let’s start working with PowerMockito API by mocking final methods. To mock final methods, not much to the surprise, we should first define final methods. Here is an example of the model we will be testing:

Simple enough, the method just returns the String which is passed to it. Now, it’s time to write our test:

We have used class names while using methods so that no confusion arises when importing the classes. Let us explain the whole lot which happened in above test:

  1. We defined a generic String message which we will be using as a parameter and expectation.
  2. We mock an instance of the system under test, ClassWithFinalMethods.
  3. whenNew() method makes sure that whenever an instance of this class is made using the new keyword by invoking a no argument constructor, this mock instance is returned instead of the real object.
  4. We invoke the no argument constructor to make an instance of the system under test.
  5. We verify that the no argument constructor was actually involved during the last step.
  6. We set an expected String when the final method is called, using the String we defined in Step 1.
  7. The final method printMessage(…) is invoked.
  8. We verify that the final method was actually called.
  9. Finally, we assert our expectations to the actual String returned to us.

Now that was a lot actually. We used a simple example so that everything makes sense. As we have explicitly used class names with static methods, this test is ready to be run.

6. Mocking static methods

We are through final methods now. It’s time to learn how static methods can be tested withPowerMockito. We make a new class named as ClassWithStaticMethod and add new methods, which are static in nature:

A similar method as earlier, the method just returns the String which is passed to it. Now, it’s time to write our test:

This test was smaller than earlier. Let us talk about each step we performed here:

  1. We defined a generic String message which we will be using as a parameter.
  2. Another generic String message, to be used as an expectation.
  3. Prepare ClassWithStaticMethod for static method test.
  4. Preparing expectations when the static method will be invoked.
  5. Invoking the static method.
  6. Verifying the expected and actual result.

Testing static method is quite simple actually. the key part is to call PowerMockito.mockStatic(…) so that PowerMockito API is enabled for the class.

7. Mocking private methods

Down to the last bit, in this section, we will test private methods. This is another awesome usage of Reflection API in Java. First, we define our system under test:

Now, defining a public message which internally calls the private message is necessary. Let us see our test to see how it makes use of reflection API:

Here, few things to point out are:

  • We start by creating a mock using Powermockito.spy(…) method.
  • Next, we make use of Reflection API by providing method name as a String parameter to when(…) method.
  • Finally, we invoke the public method which in turn invoked the private method and we verify our results using assertEquals(…) method.

8. Conclusion

In this lesson, we studied simple yet concise examples on how we can make use of PowerMockito API to stub and test private, final or static methods. This was a missing feature in Mockito, which was completed by PowerMockito API.

Related posts

Related Posts

  • 17 September

    Mockito Mock static method

    In this post, we will see about Mockito Mock static method. If you want to mock static methods, you need to use PowerMockito.PowerMockito is capable of testing private, final or static methods as it makes use of Java Reflection API. Let’s create a simple example to mock static method using powermockito. 1. Create a simple java […]

  • 17 September

    Mockito Mock Example

    In this lesson with Mockito, we will learn what is at the core of Mockito, which surprisingly is, mocks! Mock is an object that has predefined answers to method executions made during the test and has recorded expectations of these executions. Steps for creating Mockito TestNG example. Step 1: Create a simple java maven project. […]

  • 16 September

    Using Mockito with TestNG

    TestNG is an open source automated testing framework which is meant to perform unit, functional, end-to-end, integration testing. TestNG need minimum Java JDK 5. Based on the website, TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use like Annotation, Support […]

  • 16 September

    Using Mockito with JUnit

    In this lesson with Mockito, we will use another framework for complete testing, known as JUnit. According to JUnit website, JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks. With Mockito, creating mock objects is very easy. It provides many simple annotations to […]

  • 11 September

    Mockito Spy example

    In this lesson on Spy in Mockito, we will see how Spies differ from Mocks and how are these used. Adding to classpath, using Maven The fastest way to add Mockito to your project is using Maven dependency. [crayon-600912d846158252973332/] This dependency is simple enough and does not bring any additional or redundant libraries. See here for […]

  • 09 September

    Mockito Example with Best Practices

    In this lesson, we will study Mockito, a full fledged framework in Java to create test doubles or mocks. Introduction Mockito is an Open Source Mocking framework in Java and provides easy ways to create test doubles, also referred to as mocks in further writing. In Mockito, we mock behavior, not implementation by adding a dummy […]


Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.