ArrchanaMohan
ArrchanaMohan

Reputation: 2576

How to store the TestNG dataprovider values in a POJO class

I have a excel sheet which has two column as look below:

enter image description here

I used @Dataprovider (TestNG annotation) to read a data from excel sheet and pass it @Test method. And test method shows like below

@Test(dataProvider = "testautomation")
    public void getData(String userName, String password)throws Exception 
      {
        System.out.println(userName+ "\t ****");
        System.out.println(password);
      }

It works as expected, and got a proper data from excel sheet. But Is there any way I can keep those two arguments in pojo class and pass the class object in @test method?. By the way I can stop exposing the arguments like (username, password in @Test method).

May be the pojo class look like below:

public class DatObject
 {
    private String userName;
    private String password;

    //and getter setter methods.
 }

And the Test method will be like below:

@Test(dataProvider = "testautomation")
        public void getData(DataObject dataObj)throws Exception 
          {

            System.out.println(dataObj.getUserName()+ "\t ****");
            System.out.println(dataObj.getPasswrod());
          }

I want to call this pojo class object as argument in @test method, and using getter and setter methods in pojo class used to get values instead of passing username, and password in @Test method.

Any leads?

Upvotes: 1

Views: 2234

Answers (3)

Monika Jain
Monika Jain

Reputation: 11

Other solutions didn't work for me, but this one did:

Suppose you have 4 parameters being passed from DataProvider: givenName, familyName, age, gender. Define a helper class as:

public class UserDetails {
private String givenName;
private String familyName;
private String age;
private String gender;//and getter setter methods for all 4 param
}

Define DataProvider as below:

@DataProvider
public static Iterator<Object[]> getDataForUser() throws Exception {
    Object[][] testObjArray = {{ "Monika", "Jain", "30", "Female"},{ "Krishna", "Verma", "28" , "Male"}}; //You can pick this data from an excel here
    return getUserDetails(testObjArray);

}

private static Iterator<Object[]> getUserDetails(final Object[][] objArr) {
    List<UserDetails> testList = new ArrayList<>();
    for(Object[] arr : objArr) {
        UserDetails user = new UserDetails();
        user.setGivenName(arr[0].toString());
        user.setFamilyName(arr[1].toString());
        user.setAge(arr[2].toString());
        user.setGender(arr[3].toString());
        
        testList.add(user);
    }
    Collection<Object[]> dp = new ArrayList<Object[]>();
    for(UserDetails userDetails : testList){
        dp.add(new Object[]{userDetails});
    }
    return dp.iterator();
}

Now define test method as below:

@Test(dataProvider = "getDataForUser")
public void displayUserDetails(final UserDetails userDetails) throws InterruptedException {

    System.out.println("Given Name is:"+ userDetails.getGivenName());
    System.out.println("Family Name is:"+ userDetails.getFamilyName());
    System.out.println("Age is:"+ userDetails.getAge());
    System.out.println("Gender is:"+ userDetails.getGender());
    
}

Upvotes: 1

Amado Saladino
Amado Saladino

Reputation: 2692

You can totally get rid of the POJOs, imagine you have multiple types of POJOs, will you create a data provider for each POJO? Of course not, so you can use Map instead in your test method like this:

@Test(dataProvider = "dataProviderName")
void testGetUserDataAsPojo(Map user) {
    System.out.println("Name: " + (String)user.get("Name") );
}

This is even more generic, you can control what kinds of fields you have i.g. 'Name' field

but notice: your data provider methods should return Iterator<Object> data type

Upvotes: 0

Yevhen Danchenko
Yevhen Danchenko

Reputation: 1099

Your data provider method should return pojos:

@DataProvider(name = "pojoProvider")
public Object[][] createPojoData() {
 return new Object[][] {
   { new DataObject("User1", "****") },
   { new DataObject("User2", "****") },
 };
}

Then just specify it in test annotation:

@Test(dataProvider = "pojoProvider")
public void getData(DataObject dataObj)throws Exception 
{
    System.out.println(dataObj.getUserName()+ "\t ****");
    System.out.println(dataObj.getPasswrod());
}

Upvotes: 3

Related Questions