0

My property files looks like this:

demo6312623.mockable.io/testingmoke = 200;aaaa
www.google.com = 200;bbbb

I need to iterate all the properties in the files and pass the parameters like this:

Object[][] data = new Object[][] {
    { 200, "demo6312623.mockable.io/testing", "aaaa"}, 
    { 200, "www.google.com", "bbbb"}
} 

I can iterate trough the property file like this:

for (Map.Entry<Object, Object> props : props.entrySet()) {
    System.out.println((String)props.getKey() +" nnnnn "+ (String)props.getValue());        
 }

But I'm not sure how to pass these parameters to this method:

@Parameters
public static Collection < Object[] > addedNumbers() throws IOException {
    props = new Properties();
    FileInputStream fin = new FileInputStream("src/test/resources/application.properties");
    props.load(fin);

    for (Map.Entry < Object, Object > props: props.entrySet()) {
        System.out.println((String) props.getKey() + " nnnnn " + (String) props.getValue());
    }

    // not sure how to assign the values to the 2 dimensional array by  splitting from ";"
    //      Object[][] data = new Object[][]{
    //          { 200, "demo6312623.mockable.io/testing", "aaaa"}, 
    //          { 200, "www.google.com", "bbbb"}
    //      };
    return Arrays.asList(data);
}

1 Answer 1

1

You need to return a Collection type (like ArrayList) from your @Parameters method, so you can set the data as shown in the below code with inline comments:

@Parameters
public static Collection<Object[]> testData() throws Exception {

   //Load the properties file from src/test/resources
   FileInputStream fin = new FileInputStream("src/test/resources/
                                         application.properties");
   Properties props = new Properties();
   props.load(fin);

   //Create ArrayList object
   List<Object[]> testDataList = new ArrayList<>();

   String key = null;
   String[] value = null;
   String[] testData = null;
   for (Map.Entry<Object, Object> property : props.entrySet()) {
         key = (String)property.getKey();//Get the key

         //Split the property value with ';' and assign to String array
         String[] value = ((String)property.getValue()).split(";");
         testData[0] = value[0];
         testData[1]= value[1];

         //add to arraylist
         testDataList.add(testData);
    }

    // return arraylist object
    return testDataList;
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.