"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

December 21, 2010

TestNG: Passing Array of Objects in DataProvider


Now in this example, We will try an example to pass Array of Objects in the Dataprovider for TestNG test.

Please find working code and example listed below. Please run it as TestNG test in your eclipse editor


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.testng.Assert;
import org.testng.annotations.*;
import static org.testng.Assert.*;
import java.util.*;

//Example for Array of Objects
public class TestNGExample
{

 private String InputData;
 private List<String> ExpectedResults = new ArrayList<String>();
public String getInputData()
{
  return InputData;
}

public void setInputData(String inputData)
{
  InputData = inputData;
}

public List<String> getExpectedResults()
{
  return ExpectedResults;
}

public void setExpectedResults(List<String> expectedResults)
{
  ExpectedResults = expectedResults;
}


@DataProvider(name = "Array of objects Example")
public Object[][] parameterIntTestProvider()
{
  System.out.println("Start Data Provider Section");
  List<String> TestOneExpectedList = new ArrayList<String>();
  TestOneExpectedList.add("one expected");
  TestOneExpectedList.add("Three expected");
  TestOneExpectedList.add("two expected");
  TestOneExpectedList.add("four expected");

  List<String> TestTwoExpectedList = new ArrayList<String>();
  TestTwoExpectedList.add("one expected");
  TestTwoExpectedList.add("Three expected");
  TestTwoExpectedList.add("two expected");
  TestTwoExpectedList.add("four expected");

  TestNGExample[] Arrayobj = new TestNGExample[2];
  System.out.println("Declaration Successful : Assign Data Provider");
  Arrayobj[0] = new TestNGExample(); // I made mistake here, Commenting to highlight learning
  Arrayobj[0].setInputData("One");
  Arrayobj[0].setExpectedResults(TestOneExpectedList);
  System.out.println("First Assignment : " + Arrayobj[0].getInputData());

  Arrayobj[1] = new TestNGExample();
  Arrayobj[1].setInputData("Two");
  Arrayobj[1].setExpectedResults(TestTwoExpectedList);
  System.out.println("End : Assign Data Provider");

  for (int i = 0; i < 2; i++)
  {
   TestNGExample arrayobj = Arrayobj[i];
   System.out.println("Input Data is :" + arrayobj.getInputData());
   List<String> ls = arrayobj.getExpectedResults();
   Iterator it = ls.iterator();
   while (it.hasNext())
   {
    String value = (String) it.next();
    System.out.println("Value :" + value);
   }
  }
 return new Object[][] { { Arrayobj } };
}

@Test(dataProvider = "Array of objects Example")
public void TestMethodforClass(TestNGExample[] Arrayobj)
{
  System.out.println("Start Test : Array of objects Example");
  for (int i = 0; i < 2; i++)
  {
    TestNGExample arrayobj = Arrayobj[i];
    System.out.println("Input Data is :" + arrayobj.getInputData());
    List<String> ls = arrayobj.getExpectedResults();
    Iterator it = ls.iterator();
    while (it.hasNext())
    {
     String value = (String) it.next();
     System.out.println("Value :" + value);
    }
   }
  System.out.println("End Test : Array of objects Example");
 }

}

Results
Start Data Provider Section
Declaration Successful : Assign Data Provider
First Assignment : One
End : Assign Data Provider
Input Data is :One
Value :one expected
Value :Three expected
Value :two expected
Value :four expected
Input Data is :Two
Value :one expected
Value :Three expected
Value :two expected
Value :four expected
Start Test : Array of objects Example
Input Data is :One
Value :one expected
Value :Three expected
Value :two expected
Value :four expected
Input Data is :Two
Value :one expected
Value :Three expected
Value :two expected
Value :four expected
End Test : Array of objects Example
PASSED: TestMethodforClass([LTestNGExample;@1ac2f9c)


Below link was useful for working on Array of Objects.

Happy Reading!!

2 comments:

Unknown said...

Nice Content. Found really helpful. thanks buddy!

Prince said...

The mistake you have highlighted i didnt understood that , Can you please explain that?