Java: store data in properties and retrieve it later

By: Slav Kurochkin

In some projects data is dynamic, well in most projects, so what you have to do is collect data, use it while running tests and overweight it once data been change. Here is my simple solution which allow to store data in properties file:

Feature: Dynamic Data Collection

During test runs, open an application, store data and use it later one, once it been used I have to find child application and retrieve data from it, the old one must be overwritten or you might store in separate files.

Scenario: Collect Data
Given engineer finding data
Then engineer collecting data

 static Properties prop = new Properties();
    static OutputStream output = null;
    static InputStream input = null;

Here is my data collection method, which wiriting in config.properties

 public static void collectData(String appNumber, String dueDate){
        try {

            output = new FileOutputStream("config.properties");

            // set the properties value
            prop.setProperty("appNumber", appNumber);
            prop.setProperty("dueDate", dueDate);

            // save properties to project root folder
            prop.store(output, null);

        } catch (IOException io) {
            io.printStackTrace();
        } finally {
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

Scenario: Retrieve Data
Here is a method to retrieve specific data from the file.

Given engineer collected data and retrieving it
When engineer request data he is getting it

public static String getDataValue(String data) {

        String appValue = null;
        try {
            input = new FileInputStream("config.properties");
            // load a properties file
            prop.load(input);
           appValue =  prop.getProperty(data);
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return appValue;
    }
View Comments

Leave a Comment

Use markdown or basic HTML and be nice.