模拟嵌套调用时出错

时间:2017-01-09 18:47:57

标签: java mocking mockito powermock

我有一行简单的代码:

 DraftCampaignDetails createdDraft = draftCampaignI.createDraftCampaign(ConvertionUtil
                .getDraftCampaignDetailsfromCreateDraftRequest(request));

我试图像这样嘲笑:

 ConvertionUtil action1 = PowerMockito.mock(ConvertionUtil.class);
     when(action1.getDraftCampaignDetailsfromCreateDraftRequest(request)).thenReturn(details);

     when(draftCampaignI.createDraftCampaign(details)).thenReturn(details);

但是我收到了这个错误:

 when() requires an argument which has to be 'a method call on a mock'.

添加整个测试类以获得更清晰:

public class DraftCampaignActivityTest {
@Mock 
IDraftCampaign draftCampaignI;

/*  @Mock 
ConvertionUtil util;*/

@Before
 @SuppressWarnings("unchecked")
 public void setup()
    {
     MockitoAnnotations.initMocks(this);
    }

 @Test

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

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

     when(draftCampaignI.createDraftCampaign(details)).thenReturn(details);
    // when(util.getDraftCampaignDetailsfromCreateDraftRequest(request)).thenReturn(details);




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



 }

 private DraftCampaignDetails buildDraftDetails() {
     DraftCampaignDetails details = new DraftCampaignDetails();
     details.setDraftId("ww");
     return details;
 }

 private CreateDraftCampaignRequest createRequest() {
     CreateDraftCampaignRequest request = new CreateDraftCampaignRequest();
     request.setCampaignInfo("campaignInfo");
     request.setMarketplaceId("adadedaedaed");

     DraftCampaignDetailsBase base = new DraftCampaignDetailsBase();
     Money money = new Money();
     money.setCurrencyCode("USD");
     money.setMillicents(10L);
     base.setCampaignBudget(money);
     base.setCampaignName("name");
     base.setDraftCampaignState("DRAFT");
     request.setDraftCampaignDetailsBase(base);
     return request;

     //request
 }

我是Mockito和Powermock的新手。请帮忙!任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

要工作draftCampaignI必须注入draftContoller,为此您需要将draftContoller声明为使用@InjectMocks注释的测试类的字段,无需再明确创建DraftCampaignActivity的实例,将其留给Mockito,如下:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import static org.mockito.Mockito.when;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
public class DraftCampaignActivityTest {

    @Mock
    IDraftCampaign draftCampaignI;

    @InjectMocks
    DraftCampaignActivity draftContoller;

    @Test
    @PrepareForTest(ConvertionUtil.class)
    public void createDraft_newDraft() {
        CreateDraftCampaignRequest request = new CreateDraftCampaignRequest();
        DraftCampaignDetails details = new DraftCampaignDetails();

        PowerMockito.mockStatic(ConvertionUtil.class);
        PowerMockito.when(
            ConvertionUtil.getDraftCampaignDetailsfromCreateDraftRequest(request)
        ).thenReturn(details);

        when(draftCampaignI.createDraftCampaign(details)).thenReturn(details);

        draftContoller.createDraftCampaign(request);
    }
}

假设班级DraftCampaignActivity的类型为:

public class DraftCampaignActivity {
    ...
    private IDraftCampaign draftCampaignI;
    ...
}

有关注释InjectMocks的更多详细信息。

NB:当我们使用@RunWith(PowerMockRunner.class)时,我们无需明确调用MockitoAnnotations.initMocks(this),因为它将在内部完成,因此方法setup无用并且可以删除。