How to pass HashMap as input in the DataProvider method

2,495 views
Skip to first unread message

Sailesh Jha

unread,
Nov 17, 2015, 1:06:00 PM11/17/15
to testng-users
Hi All,

I have one test method which requires couple of the Maps as input, like below :

    @Test(dataProvider = "getEventsAPIInput", dataProviderClass = GetEventsDataProvider.class, description = "Get Events API Test")
    public void testEvents(String testCaseId, String testCaseDescription, String testCaseType, Map<String, String> queryParams )


Now, if I want to provide inputs to this method using DataProvider class, I dont know how to pass Maps as input :

    public static Object[][] getEventsAPIInput()
    {

        Object[][] retObjArr = {

                {
                    "TC_001", "TestCase1", "positive", <How_To_Pass_Map_With_Multiple_KeyValue_Pairs>
                },         
                {
                    "TC_002", "TestCase2", "negative", <How_To_Pass_Map_With_Multiple_KeyValue_Pairs>
                },         
                {
                    "TC_003", "TestCase3", "positive", <How_To_Pass_Map_With_Multiple_KeyValue_Pairs>
                },         
                {
                    "TC_004", "TestCase4", "positive", <How_To_Pass_Map_With_Multiple_KeyValue_Pairs>
                },         
               
            };

        return retObjArr;
    }

My Input map will contain several key value pairs, and I dont want to initialize the map for each row in the DataProvider Object[][] . Can someone help how to pass the map with values ?

Krishnan Mahadevan

unread,
Nov 17, 2015, 10:18:53 PM11/17/15
to testng...@googlegroups.com
The DataProvider is going to be called only once and after that TestNG iterates between the 2D array that your data provider constructs and the @Test annotated method.

So there is nothing different from how you are passing Strings for each of your row iteration !

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

Hubert Grzeskowiak

unread,
Nov 23, 2015, 10:38:04 AM11/23/15
to testng-users
Try something like this:


    public static Object[][] getEventsAPIInput()
    {

            HashMap<String, String> queryParams1 = new HashMap<String, String>();
            queryParams1.put("my key", "my value");
            HashMap<String, String> queryParams2 = new HashMap<String, String>();

        Object[][] retObjArr = {

                {
                    "TC_001", "TestCase1", "positive", queryParams1
                },         
                {
                    "TC_002", "TestCase2", "negative", queryParams2
                }
               
            };

        return retObjArr;
    }

Harmeet Singh Gujral

unread,
Dec 8, 2016, 12:13:23 AM12/8/16
to testng-users
Hi Sailesh Jha,

hope you might have got answer to your query.

Even i am looking for a solution for same. If you have any input could you please share it.

Thanks
Harmeet

Vimal Raj

unread,
Dec 8, 2016, 12:25:35 AM12/8/16
to testng-users
Hi Harmeet,

I hope Krishnan's and Hubert's replies solved the query. What are you expecting to solve?

Thanks
Vimalraj
Reply all
Reply to author
Forward
0 new messages