简体   繁体   English

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

[英]Reading data from JSON Array for data provider in testng

I want to provide data in my dataProvider using json file which has Array in one parameter. 我想使用一个参数为Array的json文件在dataProvider中提供数据。

For single ID it works fine 对于单个ID,它可以正常工作

But

ex. 恩。 JsonFile 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: Testng Dataprovider:

// Multiple Ids //多个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;
}

you need to replace the 您需要更换

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

with

     jsonData.getJSONArray("dataset");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM