@DataProvider方法在没有兼容的返回类型的情况下正常工作

huangapple go评论54阅读模式
英文:

@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 = &quot;airlineData&quot;)
public Iterator&lt;Airline&gt; getCreateAirlineData() throws IOException {
    List&lt;LinkedHashMap&lt;String, String&gt;&gt; excelDataAsListOfMap = ExcelUtils.getExcelDataAsListOfMap(&quot;CreateAirlineData&quot;, &quot;Sheet1&quot;);
    List&lt;Airline&gt; airlineData = new ArrayList&lt;&gt;();
    for(LinkedHashMap&lt;String,String&gt; data : excelDataAsListOfMap) {
        Airline airline = Airline.builder()
                .id(Integer.parseInt(data.get(&quot;Id&quot;)))
                .name(data.get(&quot;Name&quot;))
                .country(data.get(&quot;Country&quot;))
                .logo(data.get(&quot;Logo&quot;))
                .established(data.get(&quot;Established&quot;))
                .website(data.get(&quot;Website&quot;))
                .slogan(data.get(&quot;Slogan&quot;))
                .head_quaters(data.get(&quot;HeadQuarter&quot;))
                .build();
        airlineData.add(airline);
    }
    return airlineData.iterator();
}

I am using Data Provider method in Test as

@Test(dataProvider = &quot;airlineData&quot; )
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?
@DataProvider方法在没有兼容的返回类型的情况下正常工作

答案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:

@DataProvider方法在没有兼容的返回类型的情况下正常工作

Which results in:

@DataProvider方法在没有兼容的返回类型的情况下正常工作

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).

huangapple
  • 本文由 发表于 2023年3月31日 21:16:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898988.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定