{"id":135,"date":"2017-03-03T13:20:15","date_gmt":"2017-03-03T13:20:15","guid":{"rendered":"http:\/\/loadfocus.com\/blog\/tech\/?p=135"},"modified":"2019-04-26T08:45:37","modified_gmt":"2019-04-26T08:45:37","slug":"how-to-use-the-dataprovider-in-testng-with-a-java-example","status":"publish","type":"post","link":"https:\/\/loadfocus.com\/blog\/tech\/2017\/03\/how-to-use-the-dataprovider-in-testng-with-a-java-example","title":{"rendered":"How to Use the DataProvider in TestNG with a JAVA Example"},"content":{"rendered":"<span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 2<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span><p class=\"lead\">We are going to show how to use the DataProvider in your test cases created with the TestNG unit testing framework.<br \/>\nDataProvider are used in order to create data-driven tests. Basically, it will help you to run the same test case, but with different data sets.<\/p>\n<h3>Examples of DataProviders<\/h3>\n<p>We are going to use the two dimensional object array Object[][] in order to return data and make use of it in the test case.<\/p>\n<p>In order to create a DataProvider, you need to:<br \/>\n&#8211; create a new method with the two dimensional object array Object[][] as a return type<br \/>\n&#8211; add the DataProvider annotation to the method<br \/>\n&#8211; give a name for the DataProvider<br \/>\n&#8211; return a new two dimensional object array Object[][] with the desired values for the test<\/p>\n<p>Here are some examples of values that can be provided for the DataProvider in TestNG:<\/p>\n<pre class=\"lang-java\"><code class=\"lang-java\">\n@DataProvider(name = \"provideDaysInterval\")\npublic Object[][] provideData() {\nreturn new Object[][]{{1}, {2}, {28}, {110}, {365}, {400}, {800}};\n}\n\n\n@DataProvider(name = \"invalidIds\")\npublic Object[][] provideInvalidIds() {\nreturn new Object[][]{{\"a\"}, {\"asdasdasf\"}, {\"\u00a3!@$%^&amp;amp;*^(&amp;amp;*&amp;amp;^%\u00a3$@\u00a3!\"}, {\"1\"}, {\"2332423\"}, {\"123456786543sadfgh\"}, {\"1234567890\"}};\n}\n\n@DataProvider(name = \"minMaxDates\")\npublic Object[][] provideMinMaxDateRanges() {\nreturn new Object[][]{\n{\"2013-01-04\", \"2014-01-04\", \"2014-04-04\", \"2015-07-04\"},\n{\"2013-01-04\", \"2013-04-04\", \"2014-04-04\", \"2014-07-04\"}\n};\n}\n<\/code><\/pre>\n<p>Now, you can make use of these DataProviders in your test cases by following the below steps:<\/p>\n<p>&#8211; add the dataProvider attribute to the @Test annotation, and specify which is the dataProvider that you want to use. Make sure the data types defined in the two dimensional object array Object[][] are reflected in your test method attributes, see more details about the implementation of the DataProvider in TestNG:<\/p>\n<pre class=\"lang-java\"><code class=\"lang-java\">\n@Test(groups = {\"smoke\"}, dataProvider = \"provideDaysInterval\")\npublic void test_Days_Are_Valid(int numberOfDaysInterval){\n\n\n}\n\n@Test(groups = {\"smoke\"}, dataProvider = \"minMaxDates\")\npublic void test_Data_Ranges_Validate_Min_Max(String startDateFirst, String endDateFirst, String startDateLast, String endDateLast){\n\n}\n<\/code><\/pre>\n<p>This is the way to created automated data-driven test cases with TestNG and DataProviders in Java.<\/p>\n<p>&nbsp;<\/p>\n<p>We found the use of DataProviders very useful, especially for API Testing and UI Testing with Selenium WebDriver. <a href=\"https:\/\/loadfocus.com\/blog\/2013\/09\/05\/how-to-locate-web-elements-with-selenium-webdriver\/\">More details on how to&nbsp;find web elements in&nbsp;Selenium WebDriver can be found here<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p>Click here for <a href=\"https:\/\/courses.loadfocus.com\/online\/the-ultimate-selenium-webdriver-testing-hands-on-guide?id=7b5779cb1ad8cca84cc324a1d21cfcb1\">Online Tutorial for using Java and TestNG for testing with Selenium WebDriver<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 2<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span>We are going to show how to use the DataProvider in your test cases created with the TestNG unit testing framework. DataProvider are used in order to create data-driven tests. Basically, it will help you to run the same test case, but with different data sets. Examples of DataProviders We are going to use the&#8230;  <a href=\"https:\/\/loadfocus.com\/blog\/tech\/2017\/03\/how-to-use-the-dataprovider-in-testng-with-a-java-example\" class=\"more-link\" title=\"Read How to Use the DataProvider in TestNG with a JAVA Example\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22,21],"tags":[24,23],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/135"}],"collection":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/comments?post=135"}],"version-history":[{"count":3,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/135\/revisions"}],"predecessor-version":[{"id":327,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/135\/revisions\/327"}],"wp:attachment":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/media?parent=135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/categories?post=135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/tags?post=135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}