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

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

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

[/java]

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

3 thoughts on “TestNG Tutorials 55: DataProvider in TestNG – Lazy Initialisation of DataProvider Method – Use of Iterator Return Type

  1. @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();
    }

    studentDetails() should return Iterator and not Iterator, ArrayList should be changed to ArrayList

Leave a Reply

Your email address will not be published. Required fields are marked *