How to Manage Automated Test Data with JSON Files in Java

This guide shows how to store automated test data in JSON files within the resources folder, read and parse them using FastJSON and Hutool, and also retrieve configuration values from application.properties via ResourceBundle, providing reusable Java utility classes.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
How to Manage Automated Test Data with JSON Files in Java

Managing automated test data can become cumbersome when the data is hard‑coded in test scripts. Storing the data in JSON files under the resources directory simplifies maintenance, reduces duplicated code, and makes test data easy to edit.

Reading JSON Test Data

Place the JSON file (e.g., paySign.json) in src/main/resources. The following utility class reads the file, converts its content to a JSONObject using FastJSON, and returns the parsed object.

package com.xxx.jianghu.utiles;

import cn.hutool.core.io.resource.ClassPathResource;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;

public class readJson {
    public static JSONObject readJsonData(String jsonFilePath) {
        // json file is located in the resources folder
        ClassPathResource resource = new ClassPathResource(jsonFilePath);
        File file = resource.getFile();
        String fileStr = null;
        try {
            fileStr = FileUtils.readFileToString(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONObject.parseObject(fileStr);
        return jsonObject;
    }
    // Example usage:
    // System.out.println(readJsonData("paySign.json"));
}

The method accepts the relative path of the JSON file (including the .json extension). It uses ClassPathResource from Hutool to locate the file, FileUtils to read its text, and FastJSON to parse the string into a JSONObject. Any IOException is printed to the console.

Reading Configuration from application.properties

Often test code also needs values such as URLs or credentials that are stored in application.properties. The next utility class wraps ResourceBundle to fetch a property by its key.

package com.xxx.jianghu.utiles;

/* Utility for retrieving values from application.properties */
import java.util.Locale;
import java.util.ResourceBundle;

public class propertiesBundleValue {
    public static String bundleValueTools(String paramKey) {
        ResourceBundle bundle = ResourceBundle.getBundle("application", Locale.CHINA);
        return bundle.getString(paramKey);
    }
    // Example usage:
    // String url = bundleValueTools("api.url");
}

The method loads the application.properties file using the Chinese locale and returns the string associated with the supplied key. This approach centralises configuration access and avoids scattering ResourceBundle calls throughout the codebase.

Putting It All Together

With these two helpers, test code can retrieve structured test data and configuration values in a clean, reusable way:

JSONObject testData = readJson.readJsonData("paySign.json");
String apiUrl = propertiesBundleValue.bundleValueTools("api.url");
// Use testData and apiUrl in your automated tests

Remember to keep the JSON and properties files inside the resources folder so that they are packaged with the application and accessible at runtime.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaJSONfile I/Outilitytest dataproperties
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.