将列表List<DataBean>存储到二维数组Object[][].

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

Store List<DataBean> into two dimention Object[][]

问题

以下是翻译好的内容:

我正在尝试将一组 JSON 对象反序列化为 Java 对象数组。我正在使用数据提供程序将此数据组合传递给测试方法,以便测试方法对每个数据集执行。

我已经创建了如下所示的数据提供程序方法,并反序列化了 JSON:

@DataProvider(name = "listData")
public Object[][] listData() throws FileNotFoundException {
    Type listType = new TypeToken<List<DataBean>>() {}.getType();
    List<DataBean> data = new GsonBuilder().create().fromJson(new FileReader("resources/data.json"), listType);
    data.forEach(iterator -> System.out.println(iterator.getUsername() + " : : " + iterator.getPassword()));
    return new Object[][]{data.toArray()};
}

测试方法:

@Test(dataProvider = "jsonData")
public void testdata(DataBean dataBean) {
    System.out.println(dataBean.getUsername() + "============" + dataBean.getPassword());
}

以及 JSON:

[
  {
    "username": "someuser",
    "password": "abc#add"
  },
  {
    "username": "anotheruser",
    "password": "daa#add"
  }
]

不幸的是,它并没有起作用。如果我像下面这样使用强类型对象,那么它会按预期工作:

return new Object[][]{{new DataBean("user1", "d121312")},
            {new DataBean("user2", "asdsd")}};

错误信息:

org.testng.internal.reflect.MethodMatcherException: [public void com.mind.DataProviderUtil.testdata(com.mind.DataBean)] 方法未定义参数,但发现它在使用数据提供程序(无论是显式指定还是从类级别注释继承)。数据提供程序不匹配。方法:testdata([Parameter{index=0, type=com.mind.DataBean, declaredAnnotations=[]}]) 参数:[(com.mind.DataBean) com.mind.DataBean@78b66d36,(com.mind.DataBean) com.mind.DataBean@5223e5ee]

有人能帮助我将 List<DataBean> 数据存储在 Object[][] 中,以便我的测试方法可以为每组数据执行吗?

英文:

I'm trying to deserialize an array of json object to array of java Object. I'm using data provider to pass this data combination to test methods so test method executes for each data set.

I've created Data provider method as mentioned below and deserilised the Json:

@DataProvider(name = &quot;listData&quot;)
public Object[][] listData() throws FileNotFoundException {
    Type listType = new TypeToken&lt;List&lt;DataBean&gt;&gt;() {
    }.getType();
    List&lt;DataBean&gt; data = new GsonBuilder().create().fromJson(new FileReader(&quot;resources/data.json&quot;), listType);
    data.forEach(iterator -&gt; System.out.println(iterator.getUsername() + &quot; : : &quot; + iterator.getPassword()));
    return new Object[][]{data.toArray()};
}

Test method :

@Test(dataProvider = &quot;jsonData&quot;)
public void testdata(DataBean dataBean) {
    System.out.println(dataBean.getUsername() + &quot;============&quot; + dataBean.getPassword());

}

and JSON :

[
  {
    &quot;username&quot;: &quot;someuser&quot;,
    &quot;password&quot;: &quot;abc#add&quot;
  },
  {
    &quot;username&quot;: &quot;anotheruser&quot;,
    &quot;password&quot;: &quot;daa#add&quot;
  }
]

Unfortunately its not working. If i use Strong typed Object like below then its work as expected :

    return new Object[][]{{new DataBean(&quot;user1&quot;, &quot;d121312&quot;)},
            {new DataBean(&quot;user2&quot;, &quot;asdsd&quot;)}};

Error:

> org.testng.internal.reflect.MethodMatcherException: [public void
> com.mind.DataProviderUtil.testdata(com.mind.DataBean)] has no
> parameters defined but was found to be using a data provider (either
> explicitly specified or inherited from class level annotation). Data
> provider mismatch Method: testdata([Parameter{index=0,
> type=com.mind.DataBean, declaredAnnotations=[]}]) Arguments:
> [(com.mind.DataBean) com.mind.DataBean@78b66d36,(com.mind.DataBean)
> com.mind.DataBean@5223e5ee]

Can someone please help me int storing List&lt;DataBean&gt; data in Object[][] so my test method execute for each set of data

答案1

得分: 0

数据以矩阵形式存储在二维数组中。

假设有一个3x3的数组,矩阵表示如下:

1     2     1   

3     4     2

1     2     1

由于数据提供程序返回二维数组以向测试方法提供数据进行数据驱动测试。因此需要使用适当大小的Object[][]进行说明。我在JSON文件中有2组数据,并将其反序列化为我的情况下的DataBean的JAVA对象。因此在这里,我必须将大小说明为Object[dataBean.size()][1]

完整代码:

        Type listType = new TypeToken<List<DataBean>>() {
        }.getType();
        List<DataBean> bean = new GsonBuilder().create().fromJson(new FileReader("resources/data.json"), listType);
        bean.forEach(iterator -> System.out.println(iterator.getUsername() + " : : " + iterator.getPassword()));
        Object[][] data = new Object[bean.size()][1];
        for (int i = 0; i < bean.size(); i++) {
            data[i][0] = bean.get(i);
        }
        return data;
英文:

Data stores in 2-D array in matrix form.

Lets say there is an array of 3x3 then matrix representation would be

1     2     1   

3     4     2

1     2     1

As data provider return 2-D array to supply data to Test method for Data Driven test. So need to mention Object[][] with proper size. I've 2 sets of data in JSON file and I'm deserializing to a JAVA object that is DataBean in my case. So here I have to mention sizes as Object[dataBean.size()][1]

Complete Code:

    Type listType = new TypeToken&lt;List&lt;DataBean&gt;&gt;() {
    }.getType();
    List&lt;DataBean&gt; bean = new GsonBuilder().create().fromJson(new FileReader(&quot;resources/data.json&quot;), listType);
    bean.forEach(iterator -&gt; System.out.println(iterator.getUsername() + &quot; : : &quot; + iterator.getPassword()));
    Object[][] data = new Object[bean.size()][1];
    for (int i = 0; i &lt; bean.size(); i++) {
        data[i][0] = bean.get(i);
    }
    return data;

huangapple
  • 本文由 发表于 2020年9月4日 21:43:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63742386.html
匿名

发表评论

匿名网友

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

确定