3

さまざまな Web サービスを呼び出してデータベースにデータを入力するデータ サービスの単体テストに JMockit を使用しています。私は各 Web サービス呼び出しをモックし、Expectations/results を使用してサービスに返されたデータをフィードしています。

オブジェクトのリストを繰り返し処理し、毎回異なる引数を使用して Web サービスを呼び出さなければならないという問題があります。引数をキャプチャして、必要なものを返す CreateTestData メソッドにフィードできるようにします。データセットは相互に多少依存しています

テスト クラス:

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();
}

データ サービス クラス:

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

}
}

これは可能ですか?

4

1 に答える 1

2

次のようなものを使用できます。

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

次に期待で:

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

これにより、呼び出しが「記録」され、引数を「キャプチャ」できるようになります。

または呼び出し引数の検証をチェックします

私は次のようなコードを使用しました

mockedService.doMethod(with(new Object() {
  public void validate(SomeArg arg) {
    assertThat(arg.getProperty(), is(equalTo("expectation"));
  }
});
于 2012-11-06T22:26:35.940 に答える