Search code examples
testngrest-assuredrest-assured-jsonpath

pass json file to testNG Data Provider


How can I pass Json File which contains a json array to TestNG data Provider and use it in my test to parametrize my test cases. this is a Json file which I want to pass to TestNG test cases as Data Provider :

 {"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",
        }
    ]}

Here are the test and Data Provider in which I am trying to use Json file :

    @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;
    }

how can I do it so that json_array can have

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

and

jason_array[1] =  {
      "display_name":"test2",
      "type":"testlist",
      "author":null,
      "body":
      {
        "description":"test2 tl",
        "type":"GENERIC"
      },
      "perm_read":"all",
      "perm_write":"all",
    },

and so on and can be used in my test cases.


Solution

  • You can try using the below code. it worked when i used 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);
    
            }