模拟嵌套函数给NPE

时间:2017-01-09 18:26:33

标签: java unit-testing mockito powermock

嗨我在尝试编写单元测试用例时遇到Null Pointer Exception 这是课程细节:

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;
}

这是ConvertionUtil类:

 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;
}

我尝试过的是:

 @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");



 }

我正在接受NPE。我是Mockito和其他框架的新手。请帮忙!

1 个答案:

答案 0 :(得分:1)

它不起作用,因为你试图模拟一个static方法并且你没有正确地执行它,因此它调用了导致这个NPE的真实方法。

要使用static模拟Powermock方法,您需要:

  1. 在测试用例的类级别使用@RunWith(PowerMockRunner.class)注释。
  2. 在测试用例的类级别使用@PrepareForTest(ClassThatContainsStaticMethod.class)注释。
  3. 使用PowerMock.mockStatic(ClassThatContainsStaticMethod.class)模拟此类的所有方法。
  4. 所以在你的情况下,你应该有类似的东西:

    @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);
            ...
        }
    

    有关How to mock a static method with Powermock的更多详情。