将 Hashtable 数组转换为对象数组
我的问题是我无法将哈希表数组转换为对象数组。这里的内容读取读取 xls 文件中的每一行,并将其保存在哈希表数组中,格式如下:
roleName=testRole981, admin=admin, projectName=Automateme, userPassword=aspiresys12, roleDesc=grant[testplan_execute],grant [testplan_create_build],grant[testplan_metrics], adminPass=admin, userName=mur。
我想将该 hashTable 数组转换为对象数组,以便我可以将 testNG 测试用例中的那些数组作为 DataProvider 传递。
public class DriverScript {
//public boolean isExecuted = true;
Object[][] Obj = new Object[100][100];
@SuppressWarnings("unchecked")
@DataProvider(name="Test")
public Object[][] ValidDataProvider() {
Utilities util = new Utilities();
String pathValue = Utilities.LocatingXls("Data.xls");
Hashtable<String, String>[] hashDrv = (Hashtable<String, String>[]) util.contentReading(pathValue, "Scenario1");
Object[][] Obj = new Object[100][100];
for(int i=0;i<hashDrv.length;i++)
{
System.out.println("cont vector reading" + hashDrv[i].get("projectName"));
Obj[i][0] = hashDrv[i];
}
System.out.println("outsideloop" + Obj[0][0]);
return Obj;
}
@SuppressWarnings("unchecked")
@Test(dataProvider = "Test")
public void methodtest(Hashtable <String, String> a)
{
/* Utilities util = new Utilities();
String pathValue = Utilities.LocatingXls("Data.xls");
Hashtable<String, String>[] hashDrv = (Hashtable<String, String>[]) util.contentReading(pathValue, "Scenario1");
for(int i=0;i<hashDrv.length;i++)
{
System.out.println("cont vector reading" + hashDrv[i].get("projectName"));
Scenario1 scnTst=new Scenario1(hashDrv[i]);
scnTst.check1();
}
if(!isExecuted)
{
Assert.fail("falied");
}
}*/
}}
My problem here is i cant able to convert the Hashtable array into object array. here content reading reads each and every row from the xls file and saves it in the hashtable array in this format:
roleName=testRole981, admin=admin, projectName=Automateme, userPassword=aspiresys12, roleDesc=grant[testplan_execute],grant[testplan_create_build],grant[testplan_metrics], adminPass=admin, userName=mur.
I want to convert that hashTable array into an object array so that i can pass those in my testNG test cases as a DataProvider.
public class DriverScript {
//public boolean isExecuted = true;
Object[][] Obj = new Object[100][100];
@SuppressWarnings("unchecked")
@DataProvider(name="Test")
public Object[][] ValidDataProvider() {
Utilities util = new Utilities();
String pathValue = Utilities.LocatingXls("Data.xls");
Hashtable<String, String>[] hashDrv = (Hashtable<String, String>[]) util.contentReading(pathValue, "Scenario1");
Object[][] Obj = new Object[100][100];
for(int i=0;i<hashDrv.length;i++)
{
System.out.println("cont vector reading" + hashDrv[i].get("projectName"));
Obj[i][0] = hashDrv[i];
}
System.out.println("outsideloop" + Obj[0][0]);
return Obj;
}
@SuppressWarnings("unchecked")
@Test(dataProvider = "Test")
public void methodtest(Hashtable <String, String> a)
{
/* Utilities util = new Utilities();
String pathValue = Utilities.LocatingXls("Data.xls");
Hashtable<String, String>[] hashDrv = (Hashtable<String, String>[]) util.contentReading(pathValue, "Scenario1");
for(int i=0;i<hashDrv.length;i++)
{
System.out.println("cont vector reading" + hashDrv[i].get("projectName"));
Scenario1 scnTst=new Scenario1(hashDrv[i]);
scnTst.check1();
}
if(!isExecuted)
{
Assert.fail("falied");
}
}*/
}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于使用此数据提供程序的测试方法有一个参数,因此
Object[][]
数组的第二个维度应该是 1,而不是 100。也许类似于以下(未经测试)的代码:Since the test method using this data provider has one parameter, the second dimension of the
Object[][]
array should be 1, not 100. Perhaps something like this (untested) code:Hashtable
类实现Map
接口。该接口有一个名为values()
的方法 - 返回一个Collection
。然后,您可以对该集合调用 toArray() 来获取数组。示例:
问题:
Hashtable
可能不是最适合您使用的类,请尝试HashMap
。HashTable
是同步的,因此如果您不希望线程安全,那么使用该类将导致代码变慢。Map map = new HashMap();
(如果您愿意,也可以使用Hashtable
。The
Hashtable
class implements theMap
interface. This interface has a method on it calledvalues()
- that returns aCollection
. You can then calltoArray()
on that collection to get an array.Example:
Issues:
Hashtable
is probably not the best class for you to use, tryHashMap
.HashTable
is synchronized, so if you're not looking to be thread safe, using that class will result in slower code.Map map = new HashMap();
(orHashtable
if you prefer.