Bird
Raised Fist0
Selenium Javatesting~20 mins

Data providers for parameterization in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Data Provider Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a TestNG DataProvider with multiple parameters
Consider the following TestNG test method using a DataProvider. What will be the output when the test runs?
Selenium Java
import org.testng.annotations.*;

public class TestClass {
  @DataProvider(name = "dp")
  public Object[][] dataProvider() {
    return new Object[][] {
      {"user1", 1},
      {"user2", 2}
    };
  }

  @Test(dataProvider = "dp")
  public void testMethod(String username, int id) {
    System.out.println(username + "-" + id);
  }
}
Auser1-2\nuser2-1
BCompilation error due to wrong DataProvider signature
Cuser1\n1\nuser2\n2
Duser1-1\nuser2-2
Attempts:
2 left
💡 Hint
Look at how the DataProvider returns a 2D Object array and how parameters map to method arguments.
❓ assertion
intermediate
2:00remaining
Correct assertion for parameterized test with DataProvider
You have a test method that receives two integers from a DataProvider and should assert their sum equals 10. Which assertion is correct?
Selenium Java
@Test(dataProvider = "numbers")
public void testSum(int a, int b) {
  // assertion here
}

@DataProvider(name = "numbers")
public Object[][] numbers() {
  return new Object[][] {
    {7, 3},
    {5, 5},
    {6, 4}
  };
}
AAssert.assertEquals(a + b, 10);
BAssert.assertTrue(a + b == 10);
CAssert.assertEquals(10, a + b);
DAssert.assertFalse(a + b != 10);
Attempts:
2 left
💡 Hint
Remember the order of actual and expected in Assert.assertEquals(actual, expected).
🔧 Debug
advanced
2:00remaining
Identify the error in DataProvider method
What error will occur when running this TestNG test with the given DataProvider?
Selenium Java
@DataProvider(name = "dp")
public Object dataProvider() {
  return new Object[] {"test1", "test2"};
}

@Test(dataProvider = "dp")
public void testMethod(String input) {
  System.out.println(input);
}
ATest runs successfully printing test1 and test2
Bjava.lang.IllegalArgumentException: DataProvider must return Object[][] or Iterator<Object[]>
CCompilation error due to missing @Test annotation
DNullPointerException at testMethod
Attempts:
2 left
💡 Hint
Check the return type of the DataProvider method and what TestNG expects.
❓ framework
advanced
2:00remaining
Best practice for DataProvider with external data source
You want to read test data from a CSV file to use in a TestNG DataProvider. Which approach is best?
ARead the CSV file inside the DataProvider method and return Object[][] with parsed data
BHardcode CSV data inside the test method and ignore DataProvider
CManually copy CSV data into Java arrays and return from DataProvider
DUse @Parameters annotation instead of DataProvider for CSV data
Attempts:
2 left
💡 Hint
Think about maintainability and reusability of test data.
🧠 Conceptual
expert
2:00remaining
Why use DataProvider over @Parameters in TestNG?
Which statement best explains the advantage of using DataProvider for parameterization compared to @Parameters?
ADataProvider can only pass primitive types, @Parameters supports complex objects.
B@Parameters allows dynamic data generation at runtime, DataProvider requires static data only.
CDataProvider supports multiple sets of parameters for the same test method, enabling data-driven testing; @Parameters supports only single set from XML.
D@Parameters is faster and preferred for large data sets over DataProvider.
Attempts:
2 left
💡 Hint
Consider how many test runs each approach can generate from given data.

Practice

(1/5)
1. What is the main purpose of using a @DataProvider in Selenium Java tests?
easy
A. To run the same test multiple times with different input data
B. To create a new browser instance for each test
C. To skip tests that are not ready
D. To generate reports automatically after tests

Solution

  1. Step 1: Understand the role of @DataProvider

    @DataProvider supplies different sets of data to a test method, enabling multiple runs with varied inputs.
  2. Step 2: Identify the main benefit

    This helps find bugs that appear only with certain inputs by running the same test repeatedly with different data.
  3. Final Answer:

    To run the same test multiple times with different input data -> Option A
  4. Quick Check:

    @DataProvider = multiple test runs with varied data [OK]
Hint: DataProvider runs tests repeatedly with different inputs [OK]
Common Mistakes:
  • Thinking @DataProvider creates new browsers
  • Confusing @DataProvider with test skipping
  • Assuming it generates reports automatically
2. Which of the following is the correct syntax to declare a DataProvider method in Selenium Java?
easy
A. @DataProvider public void dataProviderMethod() { }
B. @Test(dataProvider = "testData") public void dataProviderMethod() { }
C. @DataProvider(name = "testData") public void dataProviderMethod() { }
D. @DataProvider(name = "testData") public Object[][] dataProviderMethod() { return new Object[][] {{"data1"}, {"data2"}}; }

Solution

  1. Step 1: Check the method signature for DataProvider

    A DataProvider method must return Object[][] and be annotated with @DataProvider(name = "...").
  2. Step 2: Verify the return type and annotation

    @DataProvider(name = "testData") public Object[][] dataProviderMethod() { return new Object[][] {{"data1"}, {"data2"}}; } correctly returns Object[][] and uses the proper annotation with a name.
  3. Final Answer:

    @DataProvider(name = "testData") public Object[][] dataProviderMethod() { return new Object[][] {{"data1"}, {"data2"}}; } -> Option D
  4. Quick Check:

    DataProvider method returns Object[][] [OK]
Hint: DataProvider methods return Object[][] with @DataProvider annotation [OK]
Common Mistakes:
  • Using void return type for DataProvider
  • Missing the name attribute in @DataProvider
  • Annotating test method instead of DataProvider method
3. Given the following code snippet, what will be the output when the test runs?
@DataProvider(name = "numbers")
public Object[][] data() {
    return new Object[][] { {1, 2}, {3, 4} };
}

@Test(dataProvider = "numbers")
public void testSum(int a, int b) {
    System.out.println(a + b);
}
medium
A. No output because test is skipped
B. 1 and 2 printed on separate lines
C. 3 and 7 printed on separate lines
D. Compilation error due to wrong DataProvider

Solution

  1. Step 1: Understand the DataProvider data

    The DataProvider returns two sets: {1, 2} and {3, 4}.
  2. Step 2: Calculate the sum for each test run

    First run: 1 + 2 = 3; second run: 3 + 4 = 7; both printed on separate lines.
  3. Final Answer:

    3 and 7 printed on separate lines -> Option C
  4. Quick Check:

    Sum of pairs = 3, 7 [OK]
Hint: Sum each data pair printed per test run [OK]
Common Mistakes:
  • Assuming only one test run happens
  • Confusing data sets with single values
  • Expecting compilation errors without syntax issues
4. Identify the error in the following DataProvider usage:
@DataProvider(name = "inputData")
public Object[] data() {
    return new Object[] {"A", "B"};
}

@Test(dataProvider = "inputData")
public void testMethod(String input) {
    System.out.println(input);
}
medium
A. DataProvider method should return Object[][], not Object[]
B. Test method parameter type is incorrect
C. Missing @Test annotation on DataProvider method
D. DataProvider name does not match test method

Solution

  1. Step 1: Check DataProvider return type

    DataProvider methods must return Object[][] to supply multiple parameter sets.
  2. Step 2: Identify mismatch in return type

    The method returns Object[], which is invalid and causes runtime errors.
  3. Final Answer:

    DataProvider method should return Object[][], not Object[] -> Option A
  4. Quick Check:

    DataProvider return type = Object[][] [OK]
Hint: DataProvider must return Object[][], not Object[] [OK]
Common Mistakes:
  • Returning Object[] instead of Object[][]
  • Confusing test method parameter types
  • Forgetting to match DataProvider name
5. You want to test a login form with multiple username and password combinations using a DataProvider. Which approach correctly handles this scenario to avoid test failures when a password is empty or null?
@DataProvider(name = "loginData")
public Object[][] loginData() {
    return new Object[][] {
        {"user1", "pass1"},
        {"user2", ""},
        {"user3", null}
    };
}

@Test(dataProvider = "loginData")
public void testLogin(String username, String password) {
    // Test logic here
}
hard
A. Remove entries with empty or null passwords from DataProvider
B. Add validation inside testLogin to skip or handle empty/null passwords
C. Change DataProvider to return only usernames without passwords
D. Use separate test methods for each username-password pair

Solution

  1. Step 1: Recognize the need to handle edge cases in test method

    Empty or null passwords can cause test failures if not handled properly.
  2. Step 2: Implement validation inside testLogin method

    Adding checks to skip or handle empty/null passwords prevents failures and tests robustness.
  3. Final Answer:

    Add validation inside testLogin to skip or handle empty/null passwords -> Option B
  4. Quick Check:

    Handle edge cases inside test method [OK]
Hint: Validate inputs inside test to handle empty or null values [OK]
Common Mistakes:
  • Removing test data instead of handling it
  • Ignoring null or empty values causing failures
  • Splitting tests unnecessarily instead of parameterizing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard