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:

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:

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.

Output:

[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)

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

If you need to add a new field or remove a field, you will get less headache to do so as you don’t need to update all Test method and very less changes in DataProvider method. You can easily add new validation or methods and use it whatever you want.

The biggest problem with a DataProvider method is that you must need to match number of arguments returned by DataProvider method and its caller Test method. If it is not same, an exception is thrown. Using above concept number of argument dependency can be eliminated.

More about TestNG in upcoming posts. Stay tuned.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning