A2oz

How to Mock Private Methods in Java Using Reflection?

Published in Java Testing 2 mins read

Mocking private methods directly using reflection in Java is not possible. Reflection allows access to public and protected members, but not private ones.

However, you can achieve similar behavior by:

  • Refactoring: The most recommended approach is to refactor the code to make the private method accessible. You can achieve this by:
    • Making the method package-private: This makes the method accessible only within the same package.
    • Creating a public wrapper method: This method calls the private method internally and allows for controlled access.
  • Using PowerMock: PowerMock is a powerful mocking framework that extends Mockito and allows mocking private methods. It uses bytecode manipulation to achieve this.

Here are some examples:

Refactoring:

// Original code
class MyClass {
    private void privateMethod() {
        // ...
    }
}

// Refactored code
class MyClass {
    void publicWrapperMethod() {
        privateMethod();
    }

    private void privateMethod() {
        // ...
    }
}

Using PowerMock:

@RunWith(PowerMockRunner.class)
@PrepareForTest(MyClass.class)
public class MyClassTest {

    @Test
    public void testPrivateMethod() throws Exception {
        // Mock the private method
        PowerMockito.mockStatic(MyClass.class);
        PowerMockito.when(MyClass.privateMethod()).thenReturn("mocked value");

        // Call the method that uses the private method
        MyClass myClass = new MyClass();
        String result = myClass.publicMethod();

        // Assert the result
        assertEquals("mocked value", result);
    }
}

Remember that mocking private methods can be challenging and might lead to code that is difficult to maintain and test. Refactoring to make methods accessible is generally the preferred approach.

Related Articles