问题
I am trying to mock a private method with power mockito, after reading this post I got some idea and I followed the same structure:
example
here is my class:
public class test(){
private long verifyMarketEligibilityAndGetOfferDeliveryCalendar(long id)
{
some lins of code for connectiong to db
}
public long createOffer(long id){
return verifyMarketEligibilityAndGetOfferDeliveryCalendar(id);
}
}
And here is my mock test:
test classUnderTest = PowerMockito.spy(new test());
PowerMockito.doReturn(10).when(classUnderTest,
"verifyMarketEligibilityAndGetOfferDeliveryCalendar", 10l);
classUnderTest.createOffer(10);
Now I expect that after calling createoffer, verifyMarketEligibilityAndGetOfferDeliveryCalendar does not invoke and instead number 10 returns but for some reason program start executing the verifyMarketEligibilityAndGetOfferDeliveryCalendar class and consequently db related code .
Can anyone help?
回答1:
PowerMockito needs those annotations to be declared.
@RunWith(PowerMockRunner.class)
@PrepareForTest(classUnderTest.class)
来源:https://stackoverflow.com/questions/35561709/can-not-mock-the-private-method-with-mockito