Home >Java >javaTutorial >How to Mock Private Methods for Testing with PowerMock and Mockito?
Mocking Private Methods for Testing with PowerMock
Testing classes with private methods can be challenging, especially when trying to assume correct behavior of those methods. PowerMock offers a solution for this, but for some users, it may not function as expected. Here, we'll explore a possible approach using a combination of Mockito and PowerMock to successfully mock private methods.
Approach:
The primary tool we'll use is Mockito's when method to specify the behavior of the private method. To access the private method, we'll utilize PowerMock's Spy feature. This allows us to create a spy object that intercepts calls to the private method and enables us to mock its behavior.
Example:
Consider the following class, CodeWithPrivateMethod, which has a private method doTheGamble.
<code class="java">public class CodeWithPrivateMethod { public void meaningfulPublicApi() { if (doTheGamble("Whatever", 1 << 3)) { throw new RuntimeException("boom"); } } private boolean doTheGamble(String whatever, int binary) { Random random = new Random(System.nanoTime()); boolean gamble = random.nextBoolean(); return gamble; } }</code>
Test:
Using the approach described, we can write a JUnit test that mocks the private method and asserts the desired behavior.
<code class="java">import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyString; import static org.powermock.api.mockito.PowerMockito.when; import static org.powermock.api.support.membermodification.MemberMatcher.method; @RunWith(PowerMockRunner.class) @PrepareForTest(CodeWithPrivateMethod.class) public class CodeWithPrivateMethodTest { @Test(expected = RuntimeException.class) public void when_gambling_is_true_then_always_explode() { CodeWithPrivateMethod spy = PowerMockito.spy(new CodeWithPrivateMethod()); when(spy, method(CodeWithPrivateMethod.class, "doTheGamble", String.class, int.class)) .withArguments(anyString(), anyInt()) .thenReturn(true); spy.meaningfulPublicApi(); } }</code>
In this test, we create a spy object spy and mock the doTheGamble method using when. We specify that for any input, the method should always return true. As a result, when the meaningfulPublicApi method is called, the test expects the code to throw a RuntimeException due to the mocked behavior of the private method.
This approach combines the power of Mockito for mocking and PowerMock for accessing private methods, providing a complete solution for testing code that relies on private methods.
The above is the detailed content of How to Mock Private Methods for Testing with PowerMock and Mockito?. For more information, please follow other related articles on the PHP Chinese website!