This is a really interesting TestNG interview question, you can expect this question if you’re having 2y to 3y experience in Automation.

Okay, Let’s see the answer to this interview question with an example…

@DataProvider is one of the powerful annotations in the TestNG annotations hierarchy, this annotation helps us to pass different sets of data to any test method.

By default, the DataProvider class will pass all the data to the test method, because the attribute “indices” value is set to “all” by default.

Indices in DataProvider

The “indices” attribute helps us to configure the @DataProvider to pass only particular data sets to the test method.

Here is the snippet…

@DataProvider(name = “test method data” indices={1,5})
Public void testDataProvider( )
// code…

Example Program to Demonstrate Indices in DataProvider:

Here is the Output:

If you focus on this line – @DataProvider(name=”Bank Customers Data”, indices= {1,3}) , I’ve set indices attribute value to 1,3 to execute 1st row and 3rd row.

Note: in Array – Index starts with “0”.