Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 01/13/2025 By admin

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)

Uncategorized

Post navigation

Previous Post: REST Assured Tutorial 58 – What is JsonPath and how to create it for simple and nested JSON Object?
Next Post: Create a Map From Excel Data in Java – Selenium – API Automation

Related Posts

Using Implicit Wait in Selenium WebDriver Uncategorized
TestNG Tutorials 40: Groups of Groups or MetaGroups in TestNG | Make Selenium Easy Uncategorized
API Testing Tutorial Part 12 – Tools For Manual & Automation API Testing Uncategorized
MSE – Make Selenium Easy Uncategorized
March 2018 – Make Selenium Easy Uncategorized
January 2018 – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark