我在将对象传递给 Testng 数据提供程序时遇到问题。
主要问题: 我正在使用https://github.com/ozlerhakan/poiji从Excel文件中读取测试数据(1行是Person类的1个新对象)。
它返回一个 Person 对象的列表,但对于数据提供者来说它应该是 Object[][]。我该如何转换它?
Xlsx 文件:
firstName | secondName
-----------------------
John | Deer
Eric | Boots
人员类别:
public class Person{
@ExcelCellName("firstName")
private String firstName;
@ExcelCellName("secondName")
private String secondName;
数据提供者:
@DataProvider
public Object[][] readExcel() throws Exception {
List<PersonalApplicant> res = Poiji.fromExcel(new File(("persons.xlsx")), Person.class);
return res;
}
错误:
Incompatible types.
Required:
java.lang.Object[][]
Found:
java.util.List
<com.example.package.Person>
最佳答案
第一个问题是您声明函数返回 Object[][] 并且返回一个列表。 声明函数仅返回 Object[],然后在返回时使用 .toArray()。
@DataProvider
public Object[] readExcel() throws Exception {
List<PersonalApplicant> res = Poiji.fromExcel(new File(("persons.xlsx")), Person.class);
return res.toArray();
}
关于java - 使用 Poiji 从 excel 获取对象列表到 Dataprovider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55847051/