'TestNg - How to merge two dataproviders horizontally
Can you please let me know if we can merge the below 2 dataproviders
public Object[][] dp1() {
User user = new User(1L, "Arun");
Transaction trx = new Transaction(1L, "atm");
return new Object[][] {
new Object[] { user , trx, true }
};
}
public Object[][] dp2() {
return new Object[][] {
new Object[] { "e", "f" }
};
}
I want to merge them horizontally, after merge the following output is desired in the merged dataprovider { user , trx, true, "e", "f" }
Solution 1:[1]
I dont think testng allows you two call two diff dataproviders. But you can write a new data provider in which you can call these methods and perform the required merging operation for each row of the array object they return. And make this method as the data provider for your tests.
public method mergeData(){
variable a [][]= dp1();
variable b [][]= dp2();
//perform required merging and store it in an object array and return it.
}
Solution 2:[2]
there is no way to set multiple data provider in testNG, so i am using a utility method to merge as below.
@DataProvider(name = "myDataProvider")
public Object[][] myDataProvider() {
return mergeDataProviders(myDataProvider1(),myDataProvider2());
}
@DataProvider(name = "myDataProvider1")
public Object[][] myDataProvider1() {
return new Object[][]{};
}
@DataProvider(name = "myDataProvider2")
public Object[][] myDataProvider2() {
return new Object[][]{};
}
private static Object[][] mergeDataProviders(Object[][] ... dataProviders) {
return Arrays.stream(args)
.map(Arrays::asList)
.flatMap(List::stream).toArray(Object[][]::new);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Shamik |
Solution 2 |