My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Links

Writing unit tests can be hard and sometimes good design has to be sacrificed for the sole purpose of testability. Often testability corresponds to good design, but this is not always the case. For example final classes and methods cannot be used, private methods sometimes need to be protected or unnecessarily moved to a collaborator, static methods should be avoided completely and so on simply because of the limitations of existing frameworks.

PowerMock is a framework that extend other mock libraries such as EasyMock with more powerful capabilities. PowerMock uses a custom classloader and bytecode manipulation to enable mocking of static methods, constructors, final classes and methods, private methods, removal of static initializers and more. By using a custom classloader no changes need to be done to the IDE or continuous integration servers which simplifies adoption. Developers familiar with the supported mock frameworks will find PowerMock easy to use, since the entire expectation API is the same, both for static methods and constructors. PowerMock aims to extend the existing API's with a small number of methods and annotations to enable the extra features. Currently PowerMock supports EasyMock and Mockito.

When writing unit tests it is often useful to bypass encapsulation and therefore PowerMock includes several features that simplifies reflection specifically useful for testing. This allows easy access to internal state, but also simplifies partial and private mocking.

Please note that PowerMock is mainly intended for people with expert knowledge in unit testing. Putting it in the hands of junior developers may cause more harm than good.

News

  • 2014-11-29: Johan blogs about the new ability to delegate to other JUnit Runners in PowerMock 1.6.0 at the Jayway Blog.
  • 2014-11-27: PowerMock 1.6.0 has been released with support for Mockito 1.10 as well as delegating the PowerMockRunner to another JUnit Runner. Big thanks to Henrik Kaipe for the excellent pull request. See change log for details.
  • 2014-09-04: PowerMock 1.5.6 has been released with bug fixes and improvements. See change log for details.

older news

Documentation

Usage

EasyMock extension

  1. Mocking static methods
  2. Mocking final methods or classes
  3. Mocking private methods
  4. Mock construction of new objects
  5. Partial Mocking
  6. Replay and verify all
  7. Mock Policies
  8. Test listeners
  9. More to come...

Mockito extension

  1. Mockito 1.8+ usage (requires PowerMock 1.3+)
  2. Mockito 1.7 usage (requires PowerMock 1.2.5)

Common

  1. Bypass encapsulation
  2. Suppress unwanted behavior
  3. Test listeners
  4. Mock Policies (examples are made with EasyMock extension API)
  5. Mock system classes (examples are made with EasyMock extension API)

TestNG

  1. TestNG usage

JUnit

  1. Delegate to another JUnit Runner

Tutorial

  1. PowerMock tutorial

Experimental

  1. Bootstrap using a JUnit Rule
  2. Bootstrap using a Java Agent

OSGi

  1. Using PowerMock in OSGi

FAQ

For questions and support please join our Google Group.


Founded by:

Other open source projects:


Powered by Google Project Hosting