1

I have a class which has a method which calls another method in the same class. I am not able to mock the another method getting called.

Here is the code I tried so far:

/*  tHis is the method under testing */
/*It calls another method getCampaignByCampaignId which is defined in the same class */
 @Transactional
public Campaign updateCampaign(Campaign campaign) throws IllegalArgumentException, InvalidInputsException, DependencyException {
    // ......

   Campaign updatedCampaign = getCampaignByCampaignId(campaignId);//This is the method in the same class
   //// ...
    return updatedCampaign;
}

Test code I tried for this function :

@PrepareForTest(IdEncryption.class)
public void updateCampaign_valid_campaign() {
    //....

    when(campaignController.getCampaignByCampaignId(CampaignTestHelper.CAMPAIGN_ID)).thenReturn(campaign);

    Campaign actualOutput = campaignController.updateCampaign(campaign);
    assertEquals(campaign, actualOutput);
    }

These are the mocks I created at the class level:

@Mock
CampaignModelBuilder campaignBuilder;

@InjectMocks
CampaignImpl campaignController;

I am getting NullPointerException because the control is going inside getCampaignByCampaignId function which is throwing NullPointerException. Ideally, I want to mock this function which I am not able to do.

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
user3681970
  • 1,201
  • 4
  • 19
  • 37
  • Try to create a *minimal* example that can be used to reproduce the issue, please don't give as huge blocks of code that are not relevant! – Nir Alfasi Jan 12 '17 at 16:09
  • @alfasin Sorry! Its 1 Am here and i am feeling bit tired so did not remove the unecessary blocks. Anyways, i updated. Please help! and let me know if any other detail is required – user3681970 Jan 12 '17 at 16:19
  • When you try to run it - what error do you get ? what is the value of `campaignId` is it the same as `CampaignTestHelper.CAMPAIGN_ID` ? where is `campaign` declared ? The way your code is currently posted - it's not possible to reproduce your issue. Please invest the time to improve the question otherwise people won't be able to help you and the question will get closed. – Nir Alfasi Jan 12 '17 at 16:24
  • 2
    I think what you're asking for is a [partial mock](http://stackoverflow.com/questions/14970516/use-mockito-to-mock-some-methods-but-not-others). – David Ehrmann Jan 12 '17 at 16:25
  • @DavidEhrmann I think that at this point all we can do is make assumptions which is a waste of time and energy. Let the OP improve the question. – Nir Alfasi Jan 12 '17 at 16:26
  • Sure. Give me sometime. I am updating with more details – user3681970 Jan 12 '17 at 16:29
  • @alfasin Yes the values are same for Campaign id. – user3681970 Jan 12 '17 at 16:36
  • 1
    Beside the technical solution: UnitTest test the *public observable behavior* in isolation, not *code*. This means the only reason why you should mock a method in the class under test is that this method is a *getter* for a (expensive) dependency. (off cause you should use DI, so that this kind of mocking is not needed in the first place. – Timothy Truckle Jan 12 '17 at 23:30
  • For the record: although that DUP question should tell you what you need - I agree with Timothy: you better rethink your design. A unit test that relies on partially mocking your **class under test**; that is not a good idea in my eyes! – GhostCat Jan 13 '17 at 07:47

0 Answers0