Categories: Selenium TopicsTestNG Tutorials

TestNG Tutorials 55: DataProvider in TestNG – Lazy Initialisation of DataProvider Method – Use of Iterator Return Type

Hello,

DataProvider is an important functionality provided by TestNG to achieve Data driven testing or providing a set of different dataset to a method to execute on. We have seen DataProvider concept in details in previous posts.

DataProvider may become nightmare if number of data (arguments) is more or it keeps changing or you do not know the required number of data in advance or number of parameters is dynamic. In all these cases, you can not define number of arguments for a DataProvider method and Test method easily. All these problem can be solved using Iterator to a List of data.

As per TestNG official document, “An Iterator lets you create your test data lazily. TestNG will invoke the iterator and then the test method with the parameters returned by this iterator one by one. This is particularly useful if you have a lot of parameter sets to pass to the method and you don’t want to create all of them upfront.”

In simple words, each iteration will be a set of data for a Test method to run on. We will see an example below:

A Student class:

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 DataProvider method with Iterator as return type and its caller Test method:

A DataProvdider method will return an Iterator of a list containing Student objects. A caller Test method needs to accept type of Object returning by Iterator i.e. Student. See an example below:

package DataProvider;

import java.util.ArrayList;
import java.util.Iterator;

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

public class DataProviderAndIterator {
        
        // A DataProvider method which returns an iterator to a list. 
        @DataProvider(name="DataProviderIterator")
        public Iterator studentDetails(){
        ArrayList s=new ArrayList();
        s.add(new Student("Kiran", 28, "Male"));
        s.add(new Student("Amod", 28, "Male"));
        s.add(new Student("Anu", 28, "Female"));
        return s.iterator();
        }

        // Test method accepting Student type attribute ( Not an Iterator)
        @Test(dataProvider = "DataProviderIterator")
        public void Studentregistration(Student student) {

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

        }

}

Output:

[RemoteTestNG] detected TestNG version 6.14.2
You are eligible to do registration.
Student registered with details as Name = Kiran , Age =28 , Gender =Male
You are eligible to do registration.
Student registered with details as Name = Amod , Age =28 , Gender =Male
You are eligible to do registration.
Student registered with details as Name = Anu , Age =28 , Gender =Female
PASSED: Studentregistration(DataProvider.Student@544fe44c)
PASSED: Studentregistration(DataProvider.Student@18eed359)
PASSED: Studentregistration(DataProvider.Student@6c3708b3)

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


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

You can see Iterator can remove the dependency of defining arguments in advance. You can easily add/remove/modify members and logics without worrying about mismatch of arguments between DataProvider method and Test method.

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

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

Recent Posts

Frequently Asked Java Program 28: Java Program to Find if Given Number Is a Perfect Number

Perfect Number: A Perfect number is a positive integer that is equal to the sum of its proper divisors excluding the number itself.…

8 hours ago

Frequently Asked Java Program 27: Java Program to Find Occurrence of Any Char in a Given String Without Iterating

Problem Statement: Find occurrence of given char  (Ignore case)  in given string without iterating through string. Input - Make Selenium Easy…

1 day ago

Frequently Asked Java Program 26: Java Program to Find Occurrence of Each Char in a Given String Without Using Collection

Problem Statement: Write a program to print occurrence of each character in given string without using Collection. Example:- Please enter…

2 days ago

TestNG Tutorials 67 : Difference Between @Ignore & exclude Attribute of @Test Method in TestNG

Hello Folks, In last post, we have learnt about new annotation of TestNG , named @Ignore which is used to…

3 days ago

TestNG Tutorials 66 : Ignoring Tests in TestNG Using New Annotation @Ignore

Hello Folks, TestNG provides an attribute called "enabled" which can be used to ignore a test in a running suite.…

4 days ago

API Testing Tutorial Part 18 – Understand HTTP Headers in API

Hello Folks, We have already learnt about : Sending GET request in Postman. Sending POST request in Postman. If you…

6 days ago