Home > Java > javaTutorial > body text

How to Mock Private Methods for Testing with PowerMock and Mockito?

Patricia Arquette
Release: 2024-11-04 04:21:29
Original
1045 people have browsed it

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template