0

I am trying to create a testNG dataprovider method which will return an array of objects of a custom class from my project. This array is a one dimensional array and I dont want to return two dimensional array from this dataprovider method. Please suggest, should Dataprovider always return a 2D array(not less than or more than 2D). If yes then I need help in the following line:

return new Object[][]{{user[0]},{user[1]},{user[2]},{user[3]}}

Can we write this line of code in any better way because if in future this array expands will have more than 4 elements than it we will have to edit this full function, Can't we use list etc?

Below is the code that I am currently using:

@DataProvider(name = "credentialsProvider", parallel=false)
public static Object[][] credentialsProvider() throws Exception {
    User[] user=new User[4];
    user[0]=new User(UserTypes.AGENCY_MANAGER,1);
    user[1]=new User(UserTypes.AGENT,1);
    user[2]=new User(UserTypes.AGENCY_MODERATOR,1);
    user[3]=new User(UserTypes.EW_ECS_AGENCY_MANAGER,1);
    return new Object[][]{{user[0]},{user[1]},{user[2]},{user[3]}};
     };
}
Nikunj Aggarwal
  • 754
  • 2
  • 12
  • 32
  • i have a wrapper that make it very easy to use would u need something like this?@DataProvider(name = "category") public Object[][] testCategoryRead() throws Exception { return provider.testDataBuilder() .withAttribute("site", siteUS).capture() .build(); } – Ran Adler Jul 13 '16 at 06:12

1 Answers1

0

There is a reason for the Dataprovider to be 2D - today you have one string you want to pass as data, if you had to pass let's say a User object and a UserDetails object to a testcase, you would need a way to pass both and hence the unit array is basically what you need to pass to single test and the 2d is for a list of such multiple data.

Why don't you just loop over your user array to add the user objects instead of going over by indices in one line. If something gets added, you just need to define the user object, the for loop would then loop over the entire length.

niharika_neo
  • 8,441
  • 1
  • 19
  • 31