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.