"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 - Passing List & Hash as Single parameter

In continuation with my previous post, Now I tried for Passing List & Hash as Single parameter. Below is the example code

Below is the working example code

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 Test1 {
@DataProvider(name = "Passing Three Parameters")
 public Object[][] createDataforTest1()
{
  System.out.println("Start Data Provider: Test1");
  Map<String, String> TestData1 = new HashMap<String, String>();
  TestData1.put("Onee", "One");
  TestData1.put("Twoo", "Two");
  Map<String, String> TestData2 = new HashMap<String, String>();
  TestData2.put("One", "One Expected");
  TestData2.put("Two", "Two Expected");
  Object[][] retkeyword = { { "One", TestData1, TestData2 },
    { "Two", TestData1, TestData2 } };
  System.out.println("End Data Provider: Test1");
  return (retkeyword);
 }
@DataProvider(name = "Passing List & Hash as Single parameter")
public Object[][] createDataforTest2()
{
  System.out.println("Start Data Provider: Test2");
  HashMap<String, List<String>> TestData3 = new HashMap<String, List<String>>();
  List<String> ls = new ArrayList<String>();
  ls.add("one expected");
  ls.add("Three expected");
  ls.add("two expected");
  ls.add("four expected");
  TestData3.put("One", ls);
  TestData3.put("Two", ls);
  Object[][] retkeyword = { { "One", TestData3 }, { "Two", TestData3 } };
  System.out.println("End Data Provider: Test2");
  return (retkeyword);
 }
 @Test(dataProvider = "Passing List & Hash as Single parameter")
 public void verifyData3(String keyword,HashMap<String, ArrayList<String>> hm)
{
  System.out.println("Start Test : Passing List & Hash as Single parameters");
  System.out.println("Keyword :" + keyword);

  List<String> ls = hm.get(keyword);
  Iterator it = ls.iterator();
while (it.hasNext())
{
   String value = (String) it.next();
   System.out.println("Value :" + value);
  }
  System.out.println("Keyword present in List " + ls.contains(keyword));
  System.out.println("End Test");
 }
@Test(dataProvider = "Passing Three Parameters")
public void verifyData3(String keyword, HashMap hM1, HashMap hM2)
{
  System.out.println("Start Test : Passing Three Parameters");
  System.out.println("Keyword :" + keyword);
  System.out.println("HashMap 1 Value is :" + hM1.get(keyword)
    + " Second HashMap value is :" + hM2.get(keyword));
  System.out.println("End Test");
 }
}
Hope it Helps!!

No comments: