08
Oct

Lazy @DataProvider initialization in Testng

Step1: Define your test data requirements as Pojo class
=======================================================
package CustomTestNGDataProvider;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

public class Student {

	private int age;
	private String name;

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Student(int age, String name) {
		super();
		this.age = age;
		this.name = name;
	}

}


Step 2:  Create your DataProvider which returns Iterator<Student> 
==================================================================

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

import org.testng.annotations.DataProvider;

public class StudentDP {

	@DataProvider(name = "studentDP")
	public Iterator<Student> myDataprovider() {
		ArrayList<Student> l = new ArrayList<>();
		l.add(new Student(10, "Sheetal"));

		return l.iterator();

	}

}

Step 3: Your Actual test
==========================
import org.testng.annotations.Test;

public class ActualTestngtest {
	
	@Test(dataProvider="studentDP", dataProviderClass=CustomTestNGDataProvider.StudentDP.class)
	public void myFunction(Student s) {
		System.out.println(s.getName()+":::::"+s.getAge());
	}
}