Partial Mock Private Method with PowerMock-Mockito

13,548

I found out the problem, the test methods expects a exception. After I modified it as follows, it is working fine.

@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassWithPrivate.class)
public class ClassWithPrivateTest {

   @Test
   public void testGetPrivateString() throws Exception {

     ClassWithPrivate spy = PowerMockito.spy(new ClassWithPrivate());

     PowerMockito.doReturn("Do").when(spy, method(ClassWithPrivate.class, "getPrivateStringWithArg", String.class)).withArguments(anyString());

   }

}
Share:
13,548
wlhee
Author by

wlhee

Updated on June 15, 2022

Comments

  • wlhee
    wlhee almost 2 years

    I am new to Mockito and PowerMock. I need to test some legacy code which has a private method I have to mock. I am considering using the private partial mocking feature from PowerMock, I tried to mimic the example from the link, but it failed. I have no idea what's wrong with it. Could you help to check it? Thanks

    Here's the class to-be-tested:

    package test;
    
    public class ClassWithPrivate
    {
    
         private String getPrivateString() {
           return "PrivateString";
         }
    
         private String getPrivateStringWithArg(String s) {
           return "PrivateStringWithArg";
         }
    
     }
    

    And This is the Test Code:

    package test;
    
    import static org.mockito.Mockito.*;
    import static org.mockito.Matchers.anyString;
    import static org.powermock.api.mockito.PowerMockito.when;
    import static org.powermock.api.support.membermodification.MemberMatcher.method;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.invocation.InvocationOnMock;
    import org.mockito.stubbing.Answer;
    import org.powermock.api.mockito.PowerMockito;
    import org.powermock.core.classloader.annotations.PrepareForTest;
    import org.powermock.api.support.membermodification.MemberMatcher;
    import org.powermock.modules.junit4.PowerMockRunner;
    
    @RunWith(PowerMockRunner.class)
    @PrepareForTest(ClassWithPrivate.class)
    public class ClassWithPrivateTest {
    
        @Test
        public void testGetPrivateString() {
    
             ClassWithPrivate spy = PowerMockito.spy(new ClassWithPrivate());
    
             PowerMockito.doReturn("Do").when(spy, method(ClassWithPrivate.class, "getPrivateStringWithArg", String.class)).withArguments(anyString());
    
        }
    
    }
    

    EDIT When I tried to compile the code, it failed with the following errors:

    ClassWithPrivateTest.java:26: unreported exception java.lang.Exception; must be caught or declared to be thrown
         PowerMockito.doReturn("Do").when(spy, method(ClassWithPrivate.class, "getPrivateStringWithArg", String.class)).withArguments(anyString());
                                         ^
    ClassWithPrivateTest.java:26: unreported exception java.lang.Exception; must be caught or declared to be thrown
         PowerMockito.doReturn("Do").when(spy, method(ClassWithPrivate.class, "getPrivateStringWithArg", String.class)).withArguments(anyString());