模拟一个方法在同一个类中调用另一个方法

时间:2017-01-12 16:06:49

标签: java unit-testing mockito powermock

我有一个类,它有一个方法可以在同一个类中调用另一个方法。我无法模仿被调用的另一种方法。

这是我到目前为止尝试的代码:

def open_after_detach_event(self, cr, uid, ids, context=None):
    ...
    return {
        'type': 'ir.actions.act_window',
        'res_model': 'calendar.event',
        'view_mode': 'form',
        'res_id': new_id,
        'target': 'current',
        'flags': {'form': {'action_buttons': True, 'options': {'mode': 'edit'}}}
    }

我为此功能尝试过的测试代码:

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

这些是我在班级创建的模拟:

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

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

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

我收到@Mock CampaignModelBuilder campaignBuilder; @InjectMocks CampaignImpl campaignController; 因为控件进入NullPointerException函数内部,而getCampaignByCampaignId投掷NullPointerException。理想情况下,我想模仿这个我无法做到的功能。

0 个答案:

没有答案