"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 19, 2010

TestNG - Assigning Data Providers using Class Members

In continuation with previous posts, We will look at passing data using class members. The following blog helped me to understand and write the example

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.*;

public class Test3
 private String InputData;
 private String ExpectedResult;
 private List<String> ls = new ArrayList<String>();

 public List<String> getLs()
  return ls;
 public void setLs(List<String> ls)
  this.ls = ls;
 // Under Source Menu Select Generate Getters & Setters - Tip from my colleague Avinash
public String getInputData()
  return InputData;

 public void setInputData(String inputData)
  InputData = inputData;

 public String getExpectedResult()
  return ExpectedResult;

 public void setExpectedResult(String expectedResult)
  ExpectedResult = expectedResult;

@DataProvider(name = "Dataprovider set with class members")
public Object[][] parameterIntTestProvider()
  Test3 obj = new Test3();
  List<String> TestList = new ArrayList<String>();
  TestList.add("one expected");
  TestList.add("Three expected");
  TestList.add("two expected");
  TestList.add("four expected");
  return new Object[][] { { obj } };

@Test(dataProvider = "Dataprovider set with class members")
public void TestMethodforClass(Test3 TestClass)
  System.out.println("Start Test : Dataprovider set with class members");
  System.out.println("Input Data is :" + TestClass.getInputData());
  System.out.println("Expected Result is :"+ TestClass.getExpectedResult());
  List<String> ls = TestClass.getLs();
  Iterator it = ls.iterator();

  while (it.hasNext())
   String value = (String) it.next();
   System.out.println("Value :" + value);
  System.out.println("End Test");

Hope it Helps!!

No comments: