使用Poiji从Excel到Dataprovider获取对象列表

时间:2019-04-25 10:22:14

标签: java selenium testng

将对象传递给Testng dataprovider时遇到问题。

主要问题: 我正在使用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>

1 个答案:

答案 0 :(得分:0)

第一个问题是您声明了要返回Object [] []的函数并返回了一个列表。 声明该函数仅返回 Object [] ,然后在返回时使用 .toArray()

    @DataProvider
    public Object[] readExcel() throws Exception {
        List<PersonalApplicant> res = Poiji.fromExcel(new File(("persons.xlsx")), Person.class);
        return res.toArray();

    }