将json文件传递给testNG数据提供程序

将json文件传递给testNG数据提供程序,testng,rest-assured,rest-assured-jsonpath,Testng,Rest Assured,Rest Assured Jsonpath,如何将包含Json数组的Json文件传递给TestNG数据提供程序,并在测试中使用它来参数化测试用例。 这是一个Json文件,我想将其作为数据提供程序传递给TestNG测试用例: {"TestList":[{ "display_name":"test1", "type":"testlist", "author":null, "body": { "description":"test1 tl",

如何将包含Json数组的Json文件传递给TestNG数据提供程序,并在测试中使用它来参数化测试用例。 这是一个Json文件,我想将其作为数据提供程序传递给TestNG测试用例:

 {"TestList":[{
      "display_name":"test1",
      "type":"testlist",
      "author":null,
      "body":
        {
          "description":"test1 tl",
          "type":"GENERIC"
        },
      "perm_read":"all",
      "perm_write":"all",
      },
        {
          "display_name":"test2",
          "type":"testlist",
          "author":null,
          "body":
          {
            "description":"test2 tl",
            "type":"GENERIC"
          },
          "perm_read":"all",
          "perm_write":"all",
        },
        {
          "display_name":"test3",
          "type":"testlist",
          "author":null,
          "body":
          {
            "description":"test3 tl",
            "type":"GENERIC"
          },
          "perm_read":"all",
          "perm_write":"all",
        }
    ]}
以下是我尝试使用Json文件的测试和数据提供程序:

    @Test(dataProvider = "body")
        public void AddTestGroup() throws InterruptedException{
           System.out.println("Parsed json ===" + TestData);

        }
 @DataProvider(name = "body")
    public Object[][] body() throws IOException{
        JSONParser parser = new JSONParser();
        JSONObject jsonObject = null;
        JSONArray json_array = null;
        try {
            Object obj = parser.parse(new FileReader("./TestDataFile.json"));
            jsonObject = (JSONObject) obj;
            json_array = (JSONArray) jsonObject.get("TestList");

        }catch (IOException e) {
            e.printStackTrace();
        }

        return json_array;
    }
我该如何做才能使json_数组

json_Array[0] = {
  "display_name":"test1",
  "type":"testlist",
  "author":null,
  "body":
    {
      "description":"test1 tl",
      "type":"GENERIC"
    },
  "perm_read":"all",
  "perm_write":"all",
  }


等等,可以在我的测试用例中使用

您可以尝试使用以下代码。当我使用你的json时,它工作了

    public class NewTest {
        @DataProvider(name = "JsonParser")

        public static Object[] credentials() {

            Object[] data = null;
// I saved the Json in a file.
            String filename = "YourFilePath";
            try {
                JsonObject jObject = new JsonParser().parse(new FileReader(new File(filename))).getAsJsonObject();
                JsonArray jArray = jObject.getAsJsonArray("TestList");
                data = new Object[jArray.size()];
                for (int i = 0; i < jArray.size(); i++) {
                    System.out.println(jArray.get(i));
                    data[i] = jArray.get(i);
                }

            } catch (Exception e) {

            }
            return data;

        }

        // Here we are calling the Data Provider object with its Name

        @Test(dataProvider = "JsonParser")

        public void test(Object arrays) {

            System.out.println("From test class" + arrays);

        }
公共类新测试{
@数据提供者(name=“JsonParser”)
公共静态对象[]凭据(){
Object[]data=null;
//我将Json保存在一个文件中。
字符串filename=“YourFilePath”;
试一试{
JsonObject jObject=new-JsonParser().parse(new-FileReader(new-File(filename))).getAsJsonObject();
JsonArray jArray=jObject.getAsJsonArray(“TestList”);
数据=新对象[jArray.size()];
for(int i=0;i
您可以尝试使用下面的代码。当我使用ur json时,它工作正常

    public class NewTest {
        @DataProvider(name = "JsonParser")

        public static Object[] credentials() {

            Object[] data = null;
// I saved the Json in a file.
            String filename = "YourFilePath";
            try {
                JsonObject jObject = new JsonParser().parse(new FileReader(new File(filename))).getAsJsonObject();
                JsonArray jArray = jObject.getAsJsonArray("TestList");
                data = new Object[jArray.size()];
                for (int i = 0; i < jArray.size(); i++) {
                    System.out.println(jArray.get(i));
                    data[i] = jArray.get(i);
                }

            } catch (Exception e) {

            }
            return data;

        }

        // Here we are calling the Data Provider object with its Name

        @Test(dataProvider = "JsonParser")

        public void test(Object arrays) {

            System.out.println("From test class" + arrays);

        }
公共类新测试{
@数据提供者(name=“JsonParser”)
公共静态对象[]凭据(){
Object[]data=null;
//我将Json保存在一个文件中。
字符串filename=“YourFilePath”;
试一试{
JsonObject jObject=new-JsonParser().parse(new-FileReader(new-File(filename))).getAsJsonObject();
JsonArray jArray=jObject.getAsJsonArray(“TestList”);
数据=新对象[jArray.size()];
for(int i=0;i
您可以使用
JsonPath
使代码更加简单

    @DataProvider(name = "body")
    public Object[] body() throws IOException{
        JsonPath path = JsonPath.from(new File("./TestDataFile.json"));
        List<HashMap<String, Object>> maps = path.getList("TestList");
        Object[] hashMaps = maps.toArray();
        return hashMaps;
    }
为了从映射中获取数据,需要使用上例中的
get()
方法

但是,对于元素
body
,您需要做更多的工作。
body
元素将被视为
HashMap
,因此要从
body
type
一样获取元素,您必须执行以下操作:

@Test(dataProvider = "body")
    public void test(HashMap<String, Object> map)
    {
        HashMap<String, String> body = map.get("body");
        String bodyType = body.get("type");
    }
测试(dataProvider=“body”) 公共无效测试(HashMap) { HashMap body=map.get(“body”); 字符串bodyType=body.get(“type”); }
希望有帮助!

您可以使用
JsonPath
使您的代码更加简单

    @DataProvider(name = "body")
    public Object[] body() throws IOException{
        JsonPath path = JsonPath.from(new File("./TestDataFile.json"));
        List<HashMap<String, Object>> maps = path.getList("TestList");
        Object[] hashMaps = maps.toArray();
        return hashMaps;
    }
为了从映射中获取数据,需要使用上例中的
get()
方法

但是,对于元素
body
,您需要做更多的工作。
body
元素将被视为
HashMap
,因此要从
body
type
一样获取元素,您必须执行以下操作:

@Test(dataProvider = "body")
    public void test(HashMap<String, Object> map)
    {
        HashMap<String, String> body = map.get("body");
        String bodyType = body.get("type");
    }
测试(dataProvider=“body”) 公共无效测试(HashMap) { HashMap body=map.get(“body”); 字符串bodyType=body.get(“type”); }
希望有帮助!

如何使用对象数组在@Test method内仅获取像display\u name这样的值?如何使用对象数组在@Test method内仅获取像display\u name这样的值?