3

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?

Community
  • 1
  • 1
HMdeveloper
  • 2,772
  • 9
  • 45
  • 74

1 Answers1

4

PowerMockito needs those annotations to be declared.

@RunWith(PowerMockRunner.class)
@PrepareForTest(classUnderTest.class)
AZ_
  • 21,688
  • 25
  • 143
  • 191