TestNG Tutorials 50: DataProvider in TestNG – Understand Basics of DataProvider & How It Works

Hello Folks,

In last post, we have seen Why do we need to use DataProvider in TestNG. Now we will see a serie of posts on DataProvider.

What is DataProvider?

  • DataProvider is an annotation to mark a method as data provider which provides data as an array of array of Objects which can be used for test or configuration method of a testng class to run on.
  • You need to use it as a normal annotation with any name (Optional). For E.g. @DataProvider(name=”IProvideData”) on top of method signature.
  • A DataProvider method can have a name so that it can be used to supply data to test methods by just mentioning its name.
  • If you do not provide name to a DataProvider method, you can call it with method name on which it was used.
  • A DataProvider annotated method returns an array of array of objects. Remember it can be any dimensional. I see people say that it should be 2D array. It is not like that.
  • Number of values passed by a DataProvider method should be equal to number of attributes of test method on which DataProvider method is used. If DataProvider methods provided three attributes, test method must accept three attributes and vise versa.
  • If attributes counts are not matching at both sides, you will get “org.testng.internal.reflect.MethodMatcherException: Data provider mismatch” exception.
  • The factor which differentiates passing data from a DataProvider method and passing data from testng.xml is that you can pass multiple set of data using DataProvider, on which test method will be run for each set of data.
  • We can execute a test method for multiple set of data using DataProvider which is not possible though Parameters annotation where we pass values from testng.xml.
  • If you are using DataProvider, you do not require testng xml to run it as we need to do in case of Parameters annotation.


Let’s see some examples here which will help you in understanding basic of DataProvider.

Scenario 1: A data provider method which provides N set of single data:

package DataProvider;

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

public class DataProviderBasics1 {

// You must need to mention data provider method name in Test method
public void methodWithSingleAttribute(String a) {


// A data provider which will provide single value to a test method thrice.
public Object[] myDataProvider() {

// Passing 3 set of data and each set contains single value
Object data[]= new Object[3];
data[0]= "Make";
data[1]= "Selenium";
data[2]= "Easy";
return data;


[xml][RemoteTestNG] detected TestNG version 6.14.2
PASSED: methodWithSingleAttribute("Make")
PASSED: methodWithSingleAttribute("Selenium")
PASSED: methodWithSingleAttribute("Easy")

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

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


  • Please note above that test method “methodWithSingleAttribute” ran three times and each time it was counted as a separate test. So Total tests run count is three.
  • First time it ran with value of “a” as “Make”, second time as “Selenium” and third time as “Easy”. This behaviour allows you to achieve data driven testing in Selenium.
  • In output, TestNg gives attribute values in brackets with method name to let you know on which data set test was run. E.g.¬†methodWithSingleAttribute(“Make”)


Below diagram will help you in understand how iteration works with DataProvider:


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.


4 thoughts on “TestNG Tutorials 50: DataProvider in TestNG – Understand Basics of DataProvider & How It Works

  1. Data Provider public java.lang.Object[] package.className.myDataProvider() must return either Object[][] or Iterator[], not class [Ljava.lang.Object;

Leave a Reply

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