0

I'm trying to return values stored in a hashmap using data provider but it's not working. I'm getting the below error. Thanks in advance.

@DataProvider(name="DP")
    public HashMap<String, String> hashdata(String data) {

          HashMap<String, String> hash_map = new HashMap<String, String>();
          hash_map.put("1", "One"); 
              hash_map.put("2", "Two"); 
              hash_map.put("3", "Three"); 


        return hash_map;

    }


@Test(dataProvider="DP")

    public void test2 (HashMap<String, String> b)
    {
        System.out.println(b.get("1"));

    }

[Utils] [ERROR] [Error] org.testng.TestNGException: Some DataProvider public java.util.HashMap pck4.DataProvidertst.hashdata(java.lang.String) parameters unresolved: at 0 type class java.lang.String

-1

There are two problems in your code:

  1. According to TestNG documentation (see here), method annotated as DataProvider can only return either array of arrays of Objects (Object[][]) or iterator of array of Objects (Iterator<Object[]>)

  2. You define String data as the parameter for your hashdata method. However at a certain point in time your code would have to get the value for this parameter from somewhere. According to TestNG documentation (see here) data providers supports native injection for the parameters of types ITestContext and Method. If you need to use parameter of String type you will have to use Guice framework. However I cannot see that you use that parameter somehow.

So basically, to fix the issue you can do the following:

@DataProvider(name="DP")
public Object[][] hashdata() {
      HashMap<String, String> hash_map = new HashMap<String, String>();
      hash_map.put("1", "One"); 
      hash_map.put("2", "Two"); 
      hash_map.put("3", "Three"); 
      return new Object[][]{
          {hash_map}
      };  
}
@Test(dataProvider="DP")
public void test2 (HashMap<String, String> b)
{
    System.out.println(b.get("1"));

}
  • Thank you Alexey. information you have provided is very helpful . – Anu Dec 19 '18 at 14:15
  • Can you please modify this example with second method that you have mentioned here ? ( with ITestContext and Method) – Anu Dec 19 '18 at 16:11
0

you can return them as array if you want or as a list

List<String> result = createListFromMapEntries(hash_map);
    return result;

and if you getting data from external resource you must declare method as static

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.