Understanding @PrepareForTest

PrepareForTest is very powerful tool that unlocks class manipulation capabilities by modifying class to byte code level.

PowerMock is a powerful library that works on byte code level of our program.

PrepareForTest annotation

PrepareForTest has one job, it will creates a reflection of your class at bytecode level. It allows you to manipulate your source code such that you can change behavior (implementation) of declared methods.

This comes handy when you are working with class or methods that are now allowed to be mocked by Java Reflection.

Here are some f the use cases for PrepareForTest:

Mock static methods:

public class ClassToTest {
 public void setUp() throws Exception {

Mock final classes/methods:

Mockito cannot mock final methods even if you have created mock of that class. To mock these methods, we need to prepare that class and mock it with PowerMockito.mock().

public class ClassToTest {
 public void testFinalMethods() throws Exception {
     final FinalClass mckFinalClass = PowerMockito.mock(FinalClass.class);

     final ClassWithFinalMethods mckClassWithFinalMethods = PowerMockito.mock(ClassWithFinalMethods.class);

Inject with PowerMockito.whenNew()

When you need to inject a mock instead of creating an actual instance of a class, you can use PowerMockito.whenNew(). This will inject your mock whenever new instance is created in your source class.

// Example Source Class
public class ExampleClass {
 public int getHeight() {
     // Lets say we have a generic method which provides different result base on the provided id
     final CustomClass customClassInstance = new CustomClass();
     return customClass.getHeight();

In this example class, we are creating CustomClass without any injection mechanism, we can inject our mock by adding ExampleClass in PrepareForTest and using PowerMockito.whenNew():

public class ExampleClassTest {

 private ExampleClass classUnderTest;

 public void setUp() throws Exception {
     // Create constructor of ExampleClass
     classUnderTest = new ExampleClass();

 public void getHeight_shoudReturnHeightOfView() throws NoSuchMethodException {
     final int expectedHeight = 100;
     // Given
     final CustomClass mckCustomClass = Mockito.mock(CustomClass.class);
     // Make sure you match exact parameters while mocking (same as we do for Mockito.mock statements).
     // There is also withArguments(first, second, ....)
     // and withAnyArguments() for cases when you don't care for arguments passed.

     final int actualResult = classUnderTest.getHeight();

     Assert.assertEquals(expectedHeight, actualResult);

To suppress(), stub() or replace() any method of a Class. You can modify behavior of methods declared in any class, You can:

suppress() methods and constructors.

// Suppress Constructor
// Suppress Method, it will return null if method has a return.

stub() methods to return different value.


replace() methods to return different value depending on parameter supplied.

PowerMockito.replace(View.class.getDeclaredMethod("someMethodThatWillReturnDifferentValue", int.class)).with(new InvocationHandler() {
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
      final int viewId = (int) args[0];

      if (viewId == ViewFactory.text_view) {
          // return mock of TextView
          return Mockito.mock(TextView.class);
      } else if (viewId == ViewFactory.image_view) {
          // return mock of ImageView
          return Mockito.mock(ImageView.class);
      // else you can call real method as well
      return method.invoke(proxy, args);

Last updated