PowerMock: Understanding @PrepareForTest

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

Table of contents

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

This blog will only talk about when and how you need to use @PrepareForTest. If you need to know more about other things PowerMock can do, read this one first

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:

  1. mock static methods
    @RunWith(PowerMockRunner.class)
    @PrepareForTest({
     ClassWithStaticMethods.class,
    })
    public class ClassToTest {
     @Before
     public void setUp() throws Exception {
         PowerMockito.mockStatic(ClassWithStaticMethods.class);
         PowerMockito.when(ClassWithStaticMethods.staticMethod()).thenReturn(someValue);
     }
    }
    
  2. 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().

    @RunWith(PowerMockRunner.class)
    @PrepareForTest({
     FinalClass.class,
     ClassWithFinalMethods.class,
    })
    public class ClassToTest {
     @Test
     public void testFinalMethods() throws Exception {
         final FinalClass mckFinalClass = PowerMockito.mock(FinalClass.class);
         PowerMockito.when(mckFinalClass.someMethod()).thenReturn(someValue);
    
         final ClassWithFinalMethods mckClassWithFinalMethods = PowerMockito.mock(ClassWithFinalMethods.class);
         PowerMockito.when(mckClassWithFinalMethods.someFinalMethod()).thenReturn(someValue);
     }
    }
    
  3. 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():

    @RunWith(PowerMockRunner.class)
    @PrepareForTest({
     ExampleClass.class
    })
    public class ExampleClassTest {
    
     private ExampleClass classUnderTest;
    
     @Before
     public void setUp() throws Exception {
         // Create constructor of ExampleClass
         classUnderTest = new ExampleClass();
     }
    
     @Test
     public void getHeight_shoudReturnHeightOfView() throws NoSuchMethodException {
         final int expectedHeight = 100;
         // Given
         final CustomClass mckCustomClass = Mockito.mock(CustomClass.class);
         Mockito.when(mckCustomClass.getHeight()).theReturn(expectedHeight);
         // 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.
         PowerMockito.whenNew(CustomClass.class).withNoArguments().thenReturn(mckCustomClass);
    
         final int actualResult = classUnderTest.getHeight();
    
         Assert.assertEquals(expectedHeight, actualResult);
     }
    }
    
  4. To suppress(), stub() or replace() any method of a Class. You can modify behavior of methods declared in any class, You can:
  5. suppress() methods and constructors.
    // Suppress Constructor
    PowerMockito.suppress(CustomLibClass.class.getConstructors());
    // Suppress Method, it will return null if method has a return.
    PowerMockito.suppress(CustomLibClass.class.getDeclaredMethod("someMethod"));
    
  6. stub() methods to return different value.
    PowerMockito.stub(CustomLibClass.class.getDeclaredMethod("someMethodThatOnlyWorksOnRumtime")).toReturn(2);
    
  7. replace() methods to return different value depending on parameter supplied.

    PowerMockito.replace(View.class.getDeclaredMethod("someMethodThatWillReturnDifferentValue", int.class)).with(new InvocationHandler() {
      @Override
      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);
      }
    });