Retrieving JSON data outside of test methods
It is often required to create a common setup or teardown method that also uses data from a JSON file. In those cases, you would not pass in a DataProvider attribute to the method, but instead call an extraction method directly.
The following code samples are a variation of the DataProvider's fetchData
method. These methods allow the user to extract the set(s) of data using rowID
and return it as a JSONObject
or JSONArray
object. These objects can then be cast to a POJO that the user defines:
// extractData_JSON method - create JSONObject containing all data sets public static JSONObject extractData_JSON(String file) throws Exception { FileReader reader = new FileReader(file); JSONParser jsonParser = new JSONParser(); return (JSONObject) jsonParser.parse(reader); }
In the preceding example, the method extracted all sets of data from the file and returned them as a JSONObject
. But users would most likely want just specific sets of data...