How to Mock Private Method in JUnit Tests Easily
To mock a private method in
JUnit, use PowerMockito which extends Mockito to support private method mocking. Annotate your test class with @RunWith(PowerMockRunner.class) and use spy() on the class instance, then use doReturn() with when() to mock the private method by name.Syntax
Use PowerMockito.spy() to create a spy of the class under test. Then mock the private method by name using PowerMockito.doReturn(mockValue).when(spy, "privateMethodName", args). Finally, call the public method that uses the private method.
@RunWith(PowerMockRunner.class): Enables PowerMockito support.@PrepareForTest(ClassName.class): Prepares the class for mocking private methods.spy(): Creates a spy object to partially mock.doReturn().when(): Defines the mocked return value for the private method.
java
import static org.powermock.api.mockito.PowerMockito.*; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) @PrepareForTest(MyClass.class) public class MyClassTest { @Test public void testPrivateMethodMock() throws Exception { MyClass spy = spy(new MyClass()); doReturn("mocked result").when(spy, "privateMethod"); String result = spy.publicMethod(); // assertions here } }
Example
This example shows how to mock a private method privateMethod inside MyClass. The public method publicMethod calls the private method. We mock the private method to return a fixed string and verify the public method uses it.
java
import static org.junit.Assert.assertEquals; import static org.powermock.api.mockito.PowerMockito.*; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; class MyClass { private String privateMethod() { return "original result"; } public String publicMethod() { return privateMethod(); } } @RunWith(PowerMockRunner.class) @PrepareForTest(MyClass.class) public class MyClassTest { @Test public void testMockPrivateMethod() throws Exception { MyClass spy = spy(new MyClass()); doReturn("mocked result").when(spy, "privateMethod"); String result = spy.publicMethod(); assertEquals("mocked result", result); } }
Output
Test passed
Common Pitfalls
- Not using
@RunWith(PowerMockRunner.class)causes PowerMockito features to fail. - Forgetting
@PrepareForTeston the class with private methods prevents mocking. - Trying to mock private methods with plain Mockito will not work.
- Not using
spy()but a normal instance will not allow partial mocking. - Incorrect method name or parameters in
doReturn().when()causes silent failures.
java
/* Wrong way: Missing @PrepareForTest and spy() */ @Test public void wrongMock() throws Exception { MyClass obj = new MyClass(); // This will NOT mock private method doReturn("mocked").when(obj, "privateMethod"); String result = obj.publicMethod(); // result will be "original result" } /* Right way: Use spy and annotations as shown in previous example */
Quick Reference
Summary tips for mocking private methods in JUnit:
- Use PowerMockito with
@RunWith(PowerMockRunner.class). - Annotate test class with
@PrepareForTestfor the target class. - Create a spy of the class instance.
- Use
doReturn(value).when(spy, "privateMethodName", args)to mock. - Call public methods that use the private method to verify behavior.
Key Takeaways
Use PowerMockito to mock private methods in JUnit tests.
Always annotate your test class with @RunWith(PowerMockRunner.class) and @PrepareForTest.
Create a spy of the class to partially mock private methods.
Use doReturn().when() with the private method name as a string to mock it.
Mocking private methods directly with plain Mockito is not supported.