TestNG Tutorials 54: DataProvider in TestNG – Implementing Object Oriented Concept “Encapsulation” with DataProvider Method

Hello Folks,

As part of ongoing series on DataProvider, we will learn “How does Encapsulation ( in OOPS concept) help in a DataProvider method? ” in this post.

Encapsulation is an Object Oriented Programming concept which describes binding of related stuffs together under one roof. It makes easier to add , remove and modify members.

For example: A Student has its name, age, gender, subject , address ( data members) and they can perform activities like Study, Playing etc (Member functions) . All these can be clubbed and kept under a roof called Student class. You can have as many as student object you want and all object will carry same properties. If you need to add or remove a field or method, you can easily do it.

Encapsulation concept is very useful if you are trying to achieve data driven testing using DataProvider methods in TestNG. We will see this concept in detail here.

Suppose you want to register 10 students with three different details like FirstName, LastName and Email_ID. You created a DataProvider method and its calling Test method as below:

package DataProvider;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
 
public class DataProviderBasics3 {
   
        
        
        // You must need to mention data provider method name in Test method
        @Test(dataProvider="DataContainer")
    public void studentRegistration(String First_name, String Last_Name, String Email_ID) {
        System.out.println("Registered student with details: "+First_name+" "+Last_Name+" "+Email_ID);
        
      
    }
     
        
        // A data provider method with return type as 2D array
    @DataProvider(name="DataContainer")
    public Object[] myDataProvider() {      
        return new Object[][] 
        {
                {"Mukesh", "Otwani", "[email protected]"},
                {"Amod","Mahajan","[email protected]"},
                {"Animesh","Prashant","[email protected]"},
                {"Ankur","Singh","[email protected]"},
                {"Amritansh", "Kumar","[email protected]"}         
                
        };
        
        
    }
}

Now you got to know that, you need to pass an extra field “Gender” to register a student. To achieve this, you need to do modification in DataProvider method along with its caller Test method to match number of arguments. Just imagine if there are more number of fields to be added or removed. It will become worse if it happens frequently.

What is the best solution to handle this pain? Answer is Encapsulation.

We will create a class named “Student” and define properties in this. We can also create some methods which can be used to do some validation, data getter and setter during registration. A sample java code is below:

package DataProvider;

public class Student {
        
        String name;
        int age;
        String gender;
        
        public Student(String name, int age, String gender)
        {
                this.name= name;
                this.age=age;
                this.gender=gender;
        }
        
        // A method to check if student is eligible for registration
        public boolean validateAge()
        {
                if(this.age>2)
                {
                        System.out.println("You are eligible to do registration.");
                        return true;
                }
                else
                        return false;
        }
        

}

A caller Test method and its DataProvider method are as below:

Below DataProvider method returns an Object. I am not specifying any list of parameters. This solves our problem as if we need to add or remove a new field, we do not need to modify anything to match number of arguments in caller Test method.

package DataProvider;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class StudentDataProvider {

        // A data provider method with return type as 2D array
        @DataProvider(name = "DataContainer")
        public static Student[] myDataProvider() {

                return new Student[] { new Student("Amod", 4, "Male"), new Student("Neha", 1, "Female")

                };

        }

        // You must need to mention data provider method name in Test method
        @Test(dataProvider = "DataContainer")
        public void methodWithSingleAttribute(Student a) {

                if(a.validateAge())
                {
                        System.out.println("Student registered with details as Name = "+a.name + " , Age ="+a.age +" , Gender ="+a.gender);
                }
                else
                {
                        System.out.println("Student not registered with details as Name = "+a.name + " , Age ="+a.age +" , Gender ="+a.gender);
                }

        }
}

Output:

[java][RemoteTestNG] detected TestNG version 6.14.2 You are eligible to do registration. Student registered with details as Name = Amod , Age =4 , Gender =Male Student not registered with details as Name = Neha , Age =1 , Gender =Female PASSED: methodWithSingleAttribute(DataProvider.Student@544fe44c)

PASSED: methodWithSingleAttribute(DataProvider.Student@18eed359)