英文:
@DataProvider method works fine without compatible return type
问题
根据TestNG文档,数据提供程序必须返回Object[][]或Iterator<Object[]>。我已经创建了返回类型为Iterator<Any Pojo Class>的数据提供程序方法。示例如下 -
@DataProvider(name = "airlineData")
public Iterator<Airline> getCreateAirlineData() throws IOException {
List<LinkedHashMap<String, String>> excelDataAsListOfMap = ExcelUtils.getExcelDataAsListOfMap("CreateAirlineData", "Sheet1");
List<Airline> airlineData = new ArrayList<>();
for (LinkedHashMap<String, String> data : excelDataAsListOfMap) {
Airline airline = Airline.builder()
.id(Integer.parseInt(data.get("Id")))
.name(data.get("Name"))
.country(data.get("Country"))
.logo(data.get("Logo"))
.established(data.get("Established"))
.website(data.get("Website"))
.slogan(data.get("Slogan"))
.head_quarters(data.get("HeadQuarter"))
.build();
airlineData.add(airline);
}
return airlineData.iterator();
}
我在测试中使用数据提供程序方法如下 -
@Test(dataProvider = "airlineData")
public void createAirlineAndVerify(Airline airline) {
Response response = createAirline(airline);
}
根据我的理解,返回类型Iterator<Airline> 不符合文档要求。但是测试正常运行,并没有显示任何编译错误。但在IntelliJ中,我注意到它显示返回类型为红色,但成功运行测试。
有人能帮助我理解这种行为吗?
英文:
As per the TestNG doc, the Data provider must return Object[][] or Iterator<Object[]>.
I have created Data provider method with the return type as Iterator<Any Pojo Class>. An example is as below -
@DataProvider(name = "airlineData")
public Iterator<Airline> getCreateAirlineData() throws IOException {
List<LinkedHashMap<String, String>> excelDataAsListOfMap = ExcelUtils.getExcelDataAsListOfMap("CreateAirlineData", "Sheet1");
List<Airline> airlineData = new ArrayList<>();
for(LinkedHashMap<String,String> data : excelDataAsListOfMap) {
Airline airline = Airline.builder()
.id(Integer.parseInt(data.get("Id")))
.name(data.get("Name"))
.country(data.get("Country"))
.logo(data.get("Logo"))
.established(data.get("Established"))
.website(data.get("Website"))
.slogan(data.get("Slogan"))
.head_quaters(data.get("HeadQuarter"))
.build();
airlineData.add(airline);
}
return airlineData.iterator();
}
I am using Data Provider method in Test as
@Test(dataProvider = "airlineData" )
public void createAirlineAndVerify(Airline airline) {
Response response = createAirline(airline);
}
As per my understanding return type, Iterator<Airline> is not correct as per documentation. But tests run fine and it does not show any compilation error. But in IntelliJ, I noticed that it shows red as return type but runs the tests successfully.
Could someone please help me to understand this behavior here?
答案1
得分: 1
以下是已翻译的部分:
"Well, I believe there is some outdated documentation for TestNg."
"嗯,我相信TestNg的文档有些过时。"
"As I can see from TestNg sources, it successfully handles the cases like yours by automatic conversion to a proper format:"
"根据我从TestNg源代码中看到的,它通过自动转换为正确的格式成功处理了类似您的情况:"
"P.S. - What you can see in red in your IDE is probably caused by TestNg plugin that uses the rules from docs to highlight problematic code (which in fact is not problematic at all)."
"附注:您在IDE中看到的红色内容可能是由TestNg插件引起的,它使用文档中的规则来突出显示有问题的代码(实际上并没有问题)。"
英文:
Well, I believe there is some outdated documentation for TestNg.
As I can see from TestNg sources, it successfully handles the cases like yours by automatic conversion to a proper format:
Which results in:
P.S. - What you can see in red in your IDE is probably caused by TestNg plugin that uses the rules from docs to highlight problematic code (which in fact is not problematic at all).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论