Reading data from json file for data provider in testng

arctic_monkey picture arctic_monkey · Jul 17, 2017 · Viewed 11.7k times · Source

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..

Answer

Krishnan Mahadevan picture Krishnan Mahadevan · Jul 19, 2017

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