2016-12-04 62 views
0

我的屬性文件看起來是這樣的:Programmaticaly分配屬性值到@參數

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

我需要遍歷在文件中的所有屬性和傳遞的參數是這樣的:

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

我可以迭代通過屬性文件是這樣的:

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

但我不知道如何通過這些參數TER值這個方法:

@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

您需要從@Parameters方法返回一個Collection類型(如ArrayList),所以你可以設置數據如在下面的代碼行內註釋:

@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; 
}