我有一个要求,我在文件夹中维护多个文件,我的dataprovider方法将读取文件集并构造要返回的最终对象数组。我的方法看起来如下,但它只读取一个文件,尽管我正在遍历所有文件。谁能告诉我如何解决这个问题?
public Object[][] getTestCaseInformationFromFile() throws Exception{
String testDataFile=mdfUtil.readFileAsString("TestDataFilesConfiguration.xml");
//retrieving directory path
String directoryPath=mdfUtil.getPostId("testData/directoryPath", testDataFile);
//retrieving file names
ArrayList al=mdfUtil.getAssociatedIDs("testData/file", testDataFile);
Object[][] data = null;
for(int i=0;i<al.size();i++){
csvFile=directoryPath+al.get(i);
System.out.println("csvFile-===="+csvFile);
log.info("Loading data provider TestCaseInformation...");
ArrayList<ArrayList<String>> lines=readCSVFile(csvFile);
int testSize = lines.size();
log.info("Test file has {} tests contained within it." ,testSize);
data = new Object[testSize][3];
for ( int k=0;k<lines.size();k++ ) {
ArrayList line=lines.get(k);
System.out.println("K values is "+k);
List testData=new ArrayList();
List validationData=new ArrayList();
testData=line.subList(line.indexOf("IDS")+1,line.indexOf("IDE"));
validationData=line.subList(line.indexOf("VDS")+1,line.indexOf("VDE"));
System.out.println("testData is==="+testData);
String testCaseName=(String)testData.get(4);
data[k][0]=testCaseName;
data[k][1]=testData;
data[k][2]=validationData;
}
}
System.out.println("data lenght is="+data.length);
return data;
}
答案 0 :(得分:0)
data
需要移出循环。当你在循环中声明它时,它不会在它之外被访问。您需要先弄清楚如何设置数组大小。你编写它的方式,你只得到最后一个CSV文件的结果,对吗?如果你想使用Object,那么这就是你要声明它的方式:
data = new Object[<some value>][3];
for(int i = 0; i < al.size(); i++){
csvFile = directoryPath + al.get(i);
System.out.println("csvFile-===="+csvFile);
log.info("Loading data provider TestCaseInformation...");
ArrayList<ArrayList<String>> lines=readCSVFile(csvFile);
int testSize = lines.size();
log.info("Test file has {} tests contained within it." ,testSize);
for ( int k=0;k<lines.size();k++ ) {
ArrayList line=lines.get(k);
System.out.println("K values is "+k);
List testData=new ArrayList();
List validationData=new ArrayList();
testData=line.subList(line.indexOf("IDS")+1,line.indexOf("IDE"));
validationData=line.subList(line.indexOf("VDS") + 1,line.indexOf("VDE"));
System.out.println("testData is==="+testData);
String testCaseName=(String)testData.get(4);
data[k][0]=testCaseName;
data[k][1]=testData;
data[k][2]=validationData;
}
}
如果是我,我会将data
声明为arraylist,用结果填充arraylist,然后转换为Object,并返回Object。这是你将arraylist转换为object的方法,所以你可以在dataprovider中返回它:
// Now that we know the size of the list, we can define the array size
Object[][] retObj = new Object[list1.size()][2];
for (int i = 0; i < list1.size(); i++) {
retObj[i][1] = list1.get(i);
}
return retObj;