@DataProvider with return type as Iterator<Object[]>

4.4k Views Asked by At

I try to execute the simple test case which call dataProvider and this dataProvider has return type as Iterator<Object[]>, I know this return type is not accept by @DataProvider method but after excute the below programm i get the blank output nothing is display in colsole,i accept at least message like the dataProvider is not exsist,please refer below program.

      public class Practice {


     @Test(dataProvider="NotWorking")
     public void testCase(Object[] obj)
      {
         System.out.println(obj[0]);
         System.out.println(obj[1]);
      }

@DataProvider(name="NotWorking")
public Iterator<Object[]> dataProvider2()
{
    List<Object[]> obj =new ArrayList<Object[]>();
    Object[] obj1=new Object[2];
    obj1[0]=new String("First_Object_First_value");
    obj1[1]=new String("First_Object_Second_value");
    Object[] obj2=new Object[2];
    obj2[0]=new String("Second_Object_First_value");
    obj1[1]=new String("Second_Object_Second_value");
    Iterator<Object[]> itr = obj.iterator();
    return itr;
       }
   }

i get the below output for above code,

[RemoteTestNG] detected TestNG version 7.4.0
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
2

There are 2 best solutions below

3
On BEST ANSWER

Your statement

dataProvider has return type as Iterator<Object[]>, i know this return type is not accept by @DataProvider method`.

This is actually NOT correct. Iterator<Object[]> is an ACCEPTED return type for a dataProvider in testNG.

So what is wrong with the above code is that, you are returning an empty Iterator. Note the changes in the below code:

@DataProvider(name="NotWorking")
public Iterator<Object[]> dataProvider2()
{
    List<Object[]> obj =new ArrayList<Object[]>();
    Object[] obj1=new Object[2];
    obj1[0]=new String("First_Object_First_value");
    obj1[1]=new String("First_Object_Second_value");
    Object[] obj2=new Object[2];
    obj2[0]=new String("Second_Object_First_value");
    obj2[1]=new String("Second_Object_Second_value"); // typo. used obj1 instead of obj2
    // You missed to add the below two lines
    obj.add(obj1);
    obj.add(obj2);
    Iterator<Object[]> itr = obj.iterator();
    return itr;
}

Now, after making the above changes, your test would work fine.

NOTE: A test method will not be invoked if the data to that method is empty.

So next question - Why was the test was marked as ran but did not print anything.

Answer: This depends on how an empty iterator is handled by testNG. There is a difference between versions 7.3.0 and 7.4.0 itself. It would output as Test run : 0 for 7.3.0 and as Test run : 1 for 7.4.0. There has been code changes in logging as part of the 7.4.0. The test method count for 7.4.0 is taken from test context context.getAllTestMethods(), which contains the test you are running. For 7.3.0 it is taken from an array m_allTestMethods (which is empty).

7
On

That is because dataProviders accept object[] as well. You can not use it for matrix data type but it is supported.

so if you have something like this :

@DataProvider(name = "test1")
public Object[][] createData1() {
 return new Object[][] {
   { "Cedric", new Integer(36) },
   { "Anne", new Integer(37)},
 };
}

can be easily replace by

@DataProvider(name = "test1")
public Object[] createData1() {
 return new Object[] {
   { "Cedric"},
   { "Anne"},
 };
}

your obj is has a list of Object arrays and you are returning obj.iterator(); which fulfils the dataProviders criteria.