gpt4 book ai didi

java - 使用 Poiji 从 excel 获取对象列表到 Dataprovider

转载 作者:行者123 更新时间:2023-12-02 06:19:11 25 4
gpt4 key购买 nike

我在将对象传递给 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com