mocking nested function is giving NPE

倾然丶 夕夏残阳落幕 提交于 2019-12-12 23:16:33

问题


Hi I am getting Null Pointer Exception while trying to write unit test cases Here is the class detail:

public CreateDraftCampaignResponse createDraftCampaign(CreateDraftCampaignRequest request) throws InvalidInputsException,
        DependencyException, UnauthorizedException {
    CreateDraftCampaignResponse draftCampaignResponse = null;

    try {
        DraftCampaignDetails createdDraft = draftCampaignI.createDraftCampaign(ConvertionUtil
                .getDraftCampaignDetailsfromCreateDraftRequest(request));
        draftCampaignResponse = new CreateDraftCampaignResponse();
        draftCampaignResponse.setDraftCampaignId(createdDraft.getDraftId());       
    }
    catch (Exception e) {
        log.error("Create Draft Campaign Exception", e);
        throw e;
    }
    return draftCampaignResponse;
}

This is the ConvertionUtil class:

 public static DraftCampaignDetails getDraftCampaignDetailsfromCreateDraftRequest(CreateDraftCampaignRequest request) {

    DraftCampaignDetails draftCampaign = new DraftCampaignDetails();

    DraftCampaignDetailsBase draftCampaignDetailsBase = request
            .getDraftCampaignDetailsBase(); (This is giving exception)
    draftCampaign.setCampaignBudget(draftCampaignDetailsBase
            .getCampaignBudget());
    draftCampaign.setCampaignName(draftCampaignDetailsBase
            .getCampaignName());
    draftCampaign.setDraftCampaignState(draftCampaignDetailsBase
            .getDraftCampaignState());

    draftCampaign.setCreatedUser(request.getUser());

    draftCampaign.setObfuscatedEntityId(request.getObfuscatedEntityId());
    draftCampaign.setCampaignInfo(request.getCampaignInfo());

    return draftCampaign;
}

Thsi is what I tried:

 @Test
 public void createDraft_newDraft() {
     DraftCampaignActivity draftContoller = new DraftCampaignActivity();

     CreateDraftCampaignRequest request = createRequest();
     DraftCampaignDetails details = buildDraftDetails();
     if(draftCampaignI == null){
         System.out.println("sccdscscd");
     }
     //ConvertionUtil action1 = PowerMockito.mock(ConvertionUtil.class);
     //PowerMockito.when(action1.getDraftCampaignDetailsfromCreateDraftRequest(request)).thenReturn(details);
     when(util.getDraftCampaignDetailsfromCreateDraftRequest(request)).thenReturn(details);
     when(draftCampaignI.createDraftCampaign(details)).thenReturn(details);




     CreateDraftCampaignResponse response = new CreateDraftCampaignResponse();
     draftContoller.createDraftCampaign(request);
     response.setDraftCampaignId(details.getDraftId());  
     Assert.assertEquals(response.getDraftCampaignId(),"ww");



 }

I am getting NPE. I am a novice in Mockito and other framework. Please help!


回答1:


It doesn't work because you try to mock a static method and you don't do it properly such that it calls the real method which leads to this NPE in your case.

To mock a static method using Powermock, you need to:

  1. Use the @RunWith(PowerMockRunner.class) annotation at the class-level of the test case.
  2. Use the @PrepareForTest(ClassThatContainsStaticMethod.class) annotation at the class-level of the test case.
  3. Use PowerMock.mockStatic(ClassThatContainsStaticMethod.class) to mock all methods of this class.

So in your case, you should have something like:

@RunWith(PowerMockRunner.class)
public class MyTestClass {
    @Test
    @PrepareForTest(ConvertionUtil.class)
    public void createDraft_newDraft() {
        ...
        PowerMockito.mockStatic(ConvertionUtil.class);
        PowerMockito.when(
            ConvertionUtil.getDraftCampaignDetailsfromCreateDraftRequest(request)
        ).thenReturn(details);
        ...
    }

More details about How to mock a static method with Powermock.



来源:https://stackoverflow.com/questions/41554497/mocking-nested-function-is-giving-npe

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