在这种情况下,如何验证私人方法?
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest(MockPrivateMethodExample.class)
public class MockPrivateMethodTest {
private MockPrivateMethodExample mockPrivateMethodExample;
@Test
public void testPrivateMethod() throws Exception {
mockPrivateMethodExample = new MockPrivateMethodExample();
MockPrivateMethodExample spy = PowerMockito.spy(mockPrivateMethodExample);
PowerMockito.doReturn("Test").when(spy, "iAmPrivate");
PowerMockito.doReturn("Test").when(spy, "callAnother");
String value = spy.getDetails();
//System.out.println(value);
PowerMockito.verifyPrivate(spy, Mockito.times(1)).invoke("callAnother");
}
}
这是我的课程
public class MockPrivateMethodExample {
public String getDetails() {
return iAmPrivate();
}
private String iAmPrivate() {
System.out.println("I am private ");
return callAnother();
}
private String callAnother() {
System.out.println("I am callAnother 11 ");
return "Call another";
}
}