Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 03/16/2025 By admin

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: [java][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)

Uncategorized

Post navigation

Previous Post: Git Tutorial 9 – Git Clone – Get Local Copy Of Remote Repository
Next Post: LeetCode – 100 Programs

Related Posts

REST Assured Tutorial 6 – Interface in OOPS – Implement As You Wish Uncategorized
TestNG Tutorials 42: Parameters In TestNG or Parameterization of Methods in a TestNG Class | Make Selenium Easy Uncategorized
Handling Frames/IFrames In Selenium WebDriver : Part 3 Uncategorized
image – Make Selenium Easy Uncategorized
URL Loading1504711122090 – Make Selenium Easy Uncategorized
image – 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