我正试图像这样使用PowerMock模拟私有方法
BalanceResponseGenerator balanceResponseGenerator = spy(new BalanceResponseGenerator());
when(balanceResponseGenerator, "getBalance",client.getParent(), request.getTerminal().getRetailPoint().getRetailNetwork(), result).thenReturn(new Balance());
但真正的方法被称为,为什么会如此呢?怎么了?我看到几个例子,所有这些都不适合我。请解释一下!
答案 0 :(得分:0)
我猜问题是你在Mockito.when(..)上调用的时候。 PowerMockito.when(..)
给出了正确的例子@RunWith(PowerMockRunner.class)
// We prepare PartialMockClass for test because it's final or we need to mock private or static methods
@PrepareForTest(PartialMockClass.class)
public class YourTestCase {
@Test
public void privatePartialMockingWithPowerMock() {
PartialMockClass classUnderTest = PowerMockito.spy(new PartialMockClass());
// use PowerMockito to set up your expectation
PowerMockito.doReturn(value).when(classUnderTest, "methodToMock", "parameter1");
// execute your test
classUnderTest.execute();
// Use PowerMockito.verify() to verify result
PowerMockito.verifyPrivate(classUnderTest, times(2)).invoke("methodToMock", "parameter1");
}
}