问题
I have a simple line of Code:
DraftCampaignDetails createdDraft = draftCampaignI.createDraftCampaign(ConvertionUtil
.getDraftCampaignDetailsfromCreateDraftRequest(request));
I am trying to mock it like this:
ConvertionUtil action1 = PowerMockito.mock(ConvertionUtil.class);
when(action1.getDraftCampaignDetailsfromCreateDraftRequest(request)).thenReturn(details);
when(draftCampaignI.createDraftCampaign(details)).thenReturn(details);
But I am getting this error:
when() requires an argument which has to be 'a method call on a mock'.
Adding entire test class for more clarity:
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
}
I am new to Mockito and Powermock. please help! Any help will be greatly appreciated!
回答1:
To work draftCampaignI
must be injected into draftContoller
, to do so you need to declare draftContoller
as a field of your test class annotated with @InjectMocks
, no need to create the instance of DraftCampaignActivity
explicitly anymore, leave it to Mockito, as next:
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);
}
}
Assuming that the class DraftCampaignActivity
is of type:
public class DraftCampaignActivity {
...
private IDraftCampaign draftCampaignI;
...
}
More details about the annotation InjectMocks.
NB: As we use @RunWith(PowerMockRunner.class)
, we have no need to call MockitoAnnotations.initMocks(this)
explicitly as it will be done internally such that the method setup
is useless and can be removed.
来源:https://stackoverflow.com/questions/41554844/error-in-mocking-nested-calls