从JSON数组读取数据以供testng中的数据提供者使用

时间:2018-10-01 10:23:06

标签: java arrays json testng testng-dataprovider

我想使用一个参数为Array的json文件在dataProvider中提供数据。

对于单个ID,效果很好

但是

例如JsonFile

    {
  "dataSet": [
    {
      "testCase": "Verify the limit of IDListwith 11 IDList",
      "IDList": ["1000394","1000418","1000438","1000463","1000464","1000491","1000519","1000525","1000526","1000537","1000549"]

    },
    {
      "testCase": "Verify the limit of ksnList with  ksn",
      "ksnList":[ "1234" ]    

    }
  ]
}

Testng Dataprovider:

//多个ID

@DataProvider
public static Object[][] getDataMul() throws FileNotFoundException, Exception {
    String path = System.getProperty("user.dir") + "\\input\\MultipleID_ValidJson.json";
       JsonElement jsonData = new JsonParser().parse(new FileReader(path));
        JsonElement dataSet = jsonData.getAsJsonObject().get("dataset");

        List<TestData_Json> testData = new Gson().fromJson(dataSet, new TypeToken<List<TestData_Json>>() {
        }.getType());
        Object[][] returnValue = new Object[testData.size()][1];
        int index = 0;
        for (Object[] each : returnValue) {
            each[0] = testData.get(index++);
        }
        return returnValue;
}

1 个答案:

答案 0 :(得分:0)

您需要替换

     jsonData.getAsJsonObject().get("dataset");

使用

     jsonData.getJSONArray("dataset");