What's new in PowerMock 1.1

We’re proud to announce that PowerMock 1.1 was released a couple of days ago and it has many new interesting features.

Framework independence

First of all the internal structure of the project has undergone major changes. PowerMock core is no longer coupled to EasyMock which means that PowerMock can now be used as a foundation for testability for other mock frameworks. Our intention is that in the future you pick a combination of a mock extension API of choice (such as EasyMock, Mockito or JMock) and a test framework of choice (such as JUnit or TestNG) to enable increased testability for your code. With the new release PowerMock has some basic support for Mockito using the Mockito extension api. This means that you can mock final and static methods, use annotations, enable partial mocking, mocking of signed classes even if they’re package private and spying of final classes and methods. Expect more features in the upcoming releases! It’s possible to create your own extension API’s as well so you can benefit from the PowerMock features in virtually any mock framework.

Test listeners

Another noteworthy feature of the new release is the test listener support. This means that you can create listeners and get notifications of various events, such as before a test method is about to execute or the outcome of an executed test method etc. The purpose of these test listeners is to provide a framework-independent way to get and react to these notifications by implementing the org.powermock.core.spi.PowerMockTestListener and pass it to the PowerMockListener annotation. PowerMock has some built-in test listeners for you to use, for example it has an AnnotationEnabler that let’s you use annotations to create your mock objects. For example consider the following piece of code:

Using the @Mock annotation eliminates the need to setup and tear-down mocks manually which minimizes repetitive test code and makes the test more readable. The AnnotationEnabler works for both the EasyMock and Mockito API’s. You can also supply the names of the methods that you wish to mock if you want to create a partial mock, for example:

This piece of code will instruct PowerMock to create a partial mock of the PersonDao where only the “getPerson” method is mocked. Since EasyMock has support for nice and strict mocks you can use the @MockNice and @MockStrict annotations to get the benefits of this.

Mock policies

One really cool feature of the 1.1 release is something that we refer to as mock policies. A Mock Policy can be used to make it easier to unit test some code with PowerMock in isolation from a certain framework. A mock policy implementation can for example suppress some methods, suppress static initializers or intercept method calls and change their return value (for example to return a mock object) for a certain framework or set of classes or interfaces. A mock policy can for example be implemented to avoid writing repetitive setup code for your tests. Say that you’re using a framework X that in order for you to test it requires that certain methods should always return a mock implementation. Perhaps some static initializers must be suppressed as well. Instead of copying this code between tests it would be a good idea to write a reusable mock policy.

PowerMock 1.1 provides three mock policies out of the box for mocking slf4j, java commons-logging and log4j. Let’s pick slf4j as an example and let’s say you have a class that looks like this:

To stub all calls to the logger you can use the Slf4j mock policy that takes care of doing all necessary setup for you. You use it like this:

Note that we don’t have do any setup at all to mock slf4j, the Slf4jMockPolicy takes care of this.

Mock policies can also be chained or nested like this:

You can of course create your own mock policies by implementing the org.powermock.core.spi.PowerMockPolicy interface. For more information on mock policies please have a look at the documentation.

Other

There are of course many other smaller features and improvements such as the ability to suppress individual fields and better support for resetting mocks in the EasyMock API. You can read about all the new features and bug fixes in the changelog. Please visit our web-page for more information and downloads.

This Post Has 2 Comments

  1. Does the new version support TestNG?

  2. Currently PowerMock only supports JUnit. We are considering to add support for TestNG. Of course, we would really appreciate any contributions!

Leave a Reply

Close Menu