Reading data from json file for data provider in testng

14,098

Here's how you do it.

The POJO class that represents each data set from your Json file would look like below:

public class TestData {
    private String testCase;
    private String username;
    private String password;
    private boolean rememberMe;

    public String getTestCase() {
        return testCase;
    }

    public void setTestCase(String testCase) {
        this.testCase = testCase;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isRememberMe() {
        return rememberMe;
    }

    public void setRememberMe(boolean rememberMe) {
        this.rememberMe = rememberMe;
    }

    @Override
    public String toString() {
        return "TestData{" +
                "testCase='" + testCase + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", rememberMe=" + rememberMe +
                '}';
    }
}

Here's how your test class would look like :

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;

public class SampleTestClass {

    @Test(dataProvider = "getData")
    public void testMethod(TestData data) {
        System.out.println(data);
    }

    @DataProvider
    public Object[][] getData() throws FileNotFoundException {
        JsonElement jsonData = new JsonParser().parse(new FileReader("src/test/resources/45146523.json"));
        JsonElement dataSet = jsonData.getAsJsonObject().get("dataSet");
        List<TestData> testData = new Gson().fromJson(dataSet, new TypeToken<List<TestData>>() {
        }.getType());
        Object[][] returnValue = new Object[testData.size()][1];
        int index = 0;
        for (Object[] each : returnValue) {
            each[0] = testData.get(index++);
        }
        return returnValue;
    }
}

PS Here I am making use of Google Gson library

Share:
14,098

Related videos on Youtube

arctic_monkey
Author by

arctic_monkey

Updated on June 04, 2022

Comments

  • arctic_monkey
    arctic_monkey almost 2 years

    I want to provide data in my dataProvider using json file. Please suggest the best approach for it, (in java.)

    eg. Json file

    {
      "dataSet": [
        {
          "testCase": "Verify error message on wrong userName",
          "username": "test",
          "password": "password",
          "rememberMe": false
        },
        {
          "testCase": "Verify error message on empty userName",
          "username": "",
          "password": "password",
          "rememberMe": false
        }
      ]
    }
    

    And data provider should ideally look like

    @DataProvider(name = "dpForIncorrectUsernameOrPassword")
    public static Object[][] incorrectUsernameOrPassword() {
        Object[][] testObjArray = JsonUtils.getJsonObjects("test.json");
        return testObjArray;
    }
    

    So that

    {
      "testCase": "Verify error message on wrong userName",
      "username": "test",
      "password": "password",
      "rememberMe": false
    }
    

    this acts one data set, and

    {
      "testCase": "Verify error message on empty userName",
      "username": "",
      "password": "password",
      "rememberMe": false
    }
    

    acts another, and so on..

    • juherr
      juherr almost 7 years
      What is the question?
  • arctic_monkey
    arctic_monkey almost 7 years
    Thanks krishnan. was helpful.
  • djangofan
    djangofan about 5 years
    Just have to say, if you're going through the effort to make a POJO, that being the case, using Jackson ObjectMapper is extremely easy and a good alternative whereas if you didn't have a POJO, I would go with GSon still.
  • Satyendra Sharma
    Satyendra Sharma almost 5 years
    Hi @djangofan, It will great if you could plz explain more or provide some code snippet on how to use jackson or Gson for TDD..Thanks in advance!