Can not mock the private method with mockito

余生颓废 提交于 2019-12-08 03:49:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!