Code Snippet

Just another Code Snippet site

[PowerMock] How-To

Mock private method

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;
    }
}
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() throws Exception {
        CodeWithPrivateMethod spy = PowerMockito.spy(new CodeWithPrivateMethod());

        when(spy, method(CodeWithPrivateMethod.class, "doTheGamble", String.class, int.class))
                .withArguments(anyString(), anyInt())
                .thenReturn(true);

        spy.meaningfulPublicApi();
    }
}


4 thoughts on “[PowerMock] How-To

  • Olivier says:

    Mock bean without 0-arg constructor (using PowerMock)

    import static org.mockito.Mockito.mock;
    
    @RunWith(PowerMockRunner.class)
    @PrepareForTest(ClassWithout0ArgConstructor.class)
    class Test {
    
    ClassWithout0ArgConstructor mock = mock(ClassWithout0ArgConstructor.class);
    
    // Do something with mock
    // ...
    }
    
    
  • Olivier says:

    Mock Void method

    import static org.mockito.Mockito.mock;
    
    @RunWith(PowerMockRunner.class)
    @PrepareForTest(Class2Mock.class)
    class Test {
    
        Class2Mockmock mock = mock(Class2Mock.class);
    
        @Before
        public void before() {
    
            // Without arguments
            doAnswer((Answer<Void>) invocation -> {
                // Do something
                return null;
            }).when(mock).aVoidMethodWithoutArguments();
    
            // With arguments
            doAnswer((Answer<Void>) invocation -> {
                // Do something
                System.out.println("called with args: " + invocation.getArguments());
                return null;
            }).when(mock).aVoidMethodWithArguments(any(String.class));
        }
    }
    
    
  • Olivier says:

    MVN dependencies with Mockito

                <dependency>
                    <groupId>org.powermock</groupId>
                    <artifactId>powermock-api-mockito2</artifactId>
                    <version>1.7.3</version>
                    <scope>test</scope>
                </dependency>
    
                <dependency>
                    <groupId>org.powermock</groupId>
                    <artifactId>powermock-module-junit4</artifactId>
                    <version>1.7.3</version>
                    <scope>test</scope>
                </dependency>
    

Leave a Reply to Olivier Cancel reply

Your email address will not be published. Required fields are marked *