Scrivio Interview library
CodingFoundations7 min read · updated 12 November 2026

Write a TestNG data provider that feeds three pairs of (input, expected) into a test of an isValidEmail method, and explain how failures are reported.

We write a method annotated @DataProvider(name = "emails") that returns an Object[][] with one row per case, here {"a@b.com", true}, {"no-at", false} and {"", false}, and a test annotated @Test(dataProvider = "emails") whose parameters match the columns. TestNG runs the test once per row and reports each invocation separately with its parameters, so one failing row does not hide the others. A provider can also return Iterator<Object[]> for lazy rows, live in another class through dataProviderClass, and run its rows in parallel with parallel = true, limited by data-provider-thread-count.

Why this question is asked

Data-driven tests are one of TestNG's best-known features, and validation methods such as isValidEmail are the classic use: one rule, many inputs. Interviewers ask for this exercise to see whether we can write it correctly from memory, and whether we know how failures are reported and what options a data provider has. In this answer, we will write the provider and the test, look at how each row is run and reported, see a failure, and then cover lazy iterators, providers in other classes, parallel rows, and loading rows from a CSV file.

1A Quick Recap of Data Providers

A data provider is a method that supplies arguments to a test method. Each row it returns is one set of arguments, and TestNG calls the test once for each row. This keeps the test logic in one place while the cases are listed as data, which is easier to read and extend than copying the same test five times.

The provider returns a two-dimensional array of objects: the outer array is the list of rows, and each inner array holds one value per test parameter, in order. The test method declares parameters with matching types, and TestNG passes them in.

2Writing the Provider and the Test

Here is the complete class with the three pairs from the question.

Java
public class EmailValidatorTest {
@DataProvider(name = "emails")
public Object[][] emails() {
return new Object[][] {
{"a@b.com", true},
{"no-at", false},
{"", false}
};
}
@Test(dataProvider = "emails")
public void validates(String input, boolean expected) {
assertEquals(EmailValidator.isValidEmail(input), expected); // TestNG order: actual, expected
}
}
// validates("a@b.com", true) PASSED
// validates("no-at", false) PASSED
// validates("", false) PASSED
// Tests run: 3, Failures: 0, Skips: 0

Notice that the name in @DataProvider and the dataProvider attribute must match exactly; a typo is reported as a missing data provider when the test runs. The second column is a boolean literal, which TestNG passes to the boolean parameter. And the assertion follows TestNG's argument order, actual first, then expected, which keeps failure messages correct.

3How Each Row Runs and Is Reported

TestNG treats each row as a separate invocation of the test method. Configuration methods marked @BeforeMethod run before each invocation, so each row gets fresh setup. Results are recorded per invocation, with the parameter values, so the report and the IDE show three entries for validates rather than one.

That matters most when something fails. Suppose a change to isValidEmail starts accepting strings without an @.

Java
// After a bad change to isValidEmail:
// validates("a@b.com", true) PASSED
// validates("no-at", false) FAILED
// java.lang.AssertionError: expected [false] but found [true]
// validates("", false) PASSED
// Tests run: 3, Failures: 1, Skips: 0
Step throughOne test method, three invocations
TestNGemails() providervalidates()
call provider
3 rows
invoke with ("a@b.com", true)
PASSED
invoke with ("no-at", false)
FAILED: expected [false] but found [true]
invoke with ("", false)
PASSED

8 messages in order. Play them, or step through them one at a time.

Each row is its own result, so one failure does not hide the others.

The failing row is identified by its parameters, and the other rows still run and pass. Compare that with a single test that loops over the cases: the first failure stops the loop, the remaining cases are never checked, and the report says only that the test failed. With a data provider we immediately see which input broke and that the others are fine.

4Lazy Rows, Shared Providers and Parallel Rows

A provider may return Iterator<Object[]> instead of an array. TestNG then asks for rows one at a time, which is useful when there are many rows or each one is expensive to build, because they are not all held in memory at once.

A provider can live in another class, so several test classes can share it. The test names the class with dataProviderClass, and the provider method should be static there so TestNG can call it without creating that class.

Finally, parallel = true on @DataProvider runs the rows concurrently. The size of the thread pool comes from the data-provider-thread-count attribute of the suite in testng.xml, 10 by default. That speeds up slow rows, such as rows that call a service, but the test method must then be thread-safe: no shared mutable fields.

Java
public class EmailCases {
@DataProvider(name = "emails", parallel = true)
public static Iterator<Object[]> emails() {
return List.of(
new Object[] {"a@b.com", true},
new Object[] {"no-at", false},
new Object[] {"", false}
).iterator();
}
}
@Test(dataProvider = "emails", dataProviderClass = EmailCases.class)
public void validates(String input, boolean expected) { ... }
<!-- testng.xml -->
<suite name="all" data-provider-thread-count="4"> ... </suite>

5Loading Rows From a CSV File

When the cases are maintained as a file, a provider can read them. A small CSV in src/test/resources keeps the data versioned next to the test. Reading it lazily through an iterator keeps memory low even if the file grows.

Java
# src/test/resources/emails.csv
# input,expected
a@b.com,true
no-at,false
,false
@DataProvider(name = "emailsFromCsv")
public Iterator<Object[]> emailsFromCsv() throws IOException {
var in = getClass().getResourceAsStream("/emails.csv");
var lines = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)).lines()
.filter(l -> !l.isBlank() && !l.startsWith("#"))
.map(l -> l.split(",", -1)) // -1: keep trailing empty columns
.map(c -> new Object[] {c[0], Boolean.parseBoolean(c[1])})
.toList();
return lines.iterator();
}

Notice the -1 in split. By default, Java's String.split drops trailing empty strings, so a row whose last column is empty would lose that column; with -1 every column is kept. The row with an empty input works either way, because its empty field comes first. For anything beyond simple values, such as quoted commas inside fields, a CSV library like OpenCSV or Apache Commons CSV is more reliable than split. Keeping the file small and in the repository means a reviewer sees new cases in the same pull request as the code that needs them.

6Follow-up questions

How would you load rows from a CSV file?

I would put the file in src/test/resources, read it in a @DataProvider with getResourceAsStream, skip blank and comment lines, split each line into columns, convert each column to the parameter type, and return the rows as an Iterator<Object[]>. For real CSV features such as quoted fields, I would use a CSV library instead of String.split, and I would keep the file small and versioned with the test.

What happens if the provider itself throws an exception?

TestNG cannot produce any arguments, so the test method does not run, and the invocation is reported as a failure or skip with the provider's exception, depending on the TestNG version and settings. The fix is in the provider, so its errors should be clear, for example naming the file and line that could not be parsed.

How does this compare with JUnit's parameterised tests?

JUnit Jupiter's @ParameterizedTest with @CsvSource or @MethodSource does the same job: each row becomes its own invocation with its own result. The concepts map directly, which is helpful during migrations; the main differences are annotations and that Jupiter's assertEquals takes expected first.

7Conclusion

In this answer, we wrote a TestNG data provider returning three (input, expected) rows and a test that uses it through dataProvider = "emails". TestNG runs each row as a separate invocation and reports it with its parameters, so one failing row is clearly identified while the others still run. We also saw Iterator<Object[]> for lazy rows, dataProviderClass for shared providers, parallel = true with data-provider-thread-count, and a provider that reads rows from a small CSV file.

What to say in the interviewAbout 90 seconds aloud

Open with

I would write a method annotated @DataProvider with name emails that returns an Object array of arrays, one row per case: a@b.com with true, no-at with false, and the empty string with false.

Then, if they want more

Then the test is annotated @Test with dataProvider equals emails, and takes a String input and a boolean expected, and asserts with TestNG's assertEquals, actual first, so isValidEmail of input, then expected. The names have to match exactly. TestNG calls the provider, then invokes the test once per row, with @BeforeMethod running before each invocation.

Each invocation is reported separately with its parameter values, so if the no-at row starts failing, the report shows that row with expected false but found true, and the other rows still run and pass. That is the big advantage over looping inside one test, where the first failure hides the rest.

A few options: the provider can return an Iterator of Object arrays, so rows are produced lazily, which helps with large or expensive data. It can live in another class, referenced with dataProviderClass, and it should be static there. And parallel equals true on @DataProvider runs rows concurrently, with the thread count taken from data-provider-thread-count in testng.xml.

To load rows from a CSV, I would read a small file from test resources in the provider and return an iterator of converted rows.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Write a TestNG data provider that feeds three pairs of (input, expected) into a test of...": https://scrivio-interview-library.netlify.app/java/testng/b70a79e6dd/