chapsme
chapsme

Reputation: 63

jmockit capturing mocked method arguments

I am using JMockit for unit test a data service that calls different web services and populates the database. I am mocking each web service call, and using Expectations/results to feed the service my return data.

I am having a problem where I have to iterate over a list of objects and call a web service each time using a different argument. I want to capture the argument so I can feed it into a CreateTestData method that will return what I want. The dataSets are somewhat dependent on each other

The test Class:

public class testDataService {  

@Mocked 
private WebService1 webServiceClientMocked1;

@Mocked 
private WebService2 webServiceClientMocked2;

@Autowired
private DataService dataService;

@Test
public void createTestData() {

final DataSet1 dataSet1 = CreateMyTestData.createDataSet1();
final DataSet1 dataSet2 = CreateMyTestData.createDataSet2();

 // these are populated using other methods not shown
final List<String> listStrings = new ArrayList<String>();
final List<String> entities = new ArrayList<String>();  

new Expectations() {{
        webServiceClientMocked1.getDataSet1("stringA", true);
        result = dataSet1;
    }};

new Expectations() {{
        webServiceClientMocked2.getDataSet2("stringB");
        result = dataSet2;
    }};

new Expectations() {{
for(String s : listStrings){
    webServiceClientMocked1.getDataSet4(s,(List<String>) any);
    returns(CreateMyTestData.createDataSet4(s, entities));
        }

    }};


//doesnt work       
//new Expectations() {{
//webServiceClientMocked1.findDataPerParamters(anyString, (List<String>) any );
//result = CreateMyTestData.createDataSet4(capturedString, capturedListStrings);
//}};

//call data service to test
dataService.doSaveData();
}

Data Service class:

public class DataServiceImpl implements DataService
{

public void doSaveData() {

  //do a bunch of stuff
  dataSet1 =webServiceClientMocked1.getDataSet1("stringA", true);
  //do more stuff
  dataSet2 = webServiceClientMocked2.getDataSet2("stringB");
  Collection<Stuff> dataSet3 = saveToDB(dataSet2);   //save data and return a different set of data

  for(Stuff data : dataSet3) {

  //take dataSet3, iterate over it and call another webservice
  dataSet4 = webServiceClientMocked1.getDataSet4(data.getStringX(), data.getListStrings());

 // keep doing more junk

}
}

Is this possible?

Upvotes: 3

Views: 7179

Answers (1)

jdevelop
jdevelop

Reputation: 12296

you can use something like:

final List<String> args = Arrays.asList("1","2","3","4");
final Map<String,Object> results = ....

then in Expectations:

new Expectations() {
{
  for (arg : args) {
    mockedService.invoke(arg);
    returning(results.get(arg);
  }
}

this will "record" your invocations and allow you to "capture" arguments.

or check Validating invocation arguments

I used the code like

mockedService.doMethod(with(new Object() {
  public void validate(SomeArg arg) {
    assertThat(arg.getProperty(), is(equalTo("expectation"));
  }
});

Upvotes: 2

Related Questions