gpt4 book ai didi

java - 模拟嵌套调用时出错

转载 作者:行者123 更新时间:2023-11-30 06:57:22 27 4
gpt4 key购买 nike

我有一行简单的代码:

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

我试图像这样 mock 它:

 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 的新手。请帮忙!任何帮助将不胜感激!

最佳答案

要工作,draftCampaignI 必须注入(inject)到 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 .

注意:当我们使用@RunWith(PowerMockRunner.class)时,我们不需要显式调用MockitoAnnotations.initMocks(this)因为它将在内部完成,因此方法 setup 毫无用处,可以删除。

关于java - 模拟嵌套调用时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41554844/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com