{"id":590,"date":"2009-08-09T21:49:58","date_gmt":"2009-08-10T06:49:58","guid":{"rendered":"https:\/\/www.kellyrob99.com\/blog\/?p=590"},"modified":"2009-08-09T21:49:58","modified_gmt":"2009-08-10T06:49:58","slug":"using-the-testng-dataprovider-with-groovy","status":"publish","type":"post","link":"https:\/\/www.kellyrob99.com\/blog\/2009\/08\/09\/using-the-testng-dataprovider-with-groovy\/","title":{"rendered":"Using the TestNG DataProvider with Groovy"},"content":{"rendered":"<p><a href=\"http:\/\/testng.org\/\">TestNG<\/a> is a great tool for testing in Java, and it works even better with a little Groovy thrown in. Just lately I&#8217;ve had a lot of success using the <a href=\"http:\/\/testng.org\/javadocs\/org\/testng\/annotations\/DataProvider.html\">DataProvider<\/a> pattern.<\/p>\n<p>A DataProvider method in TestNG can return either a two dimensional Object array or an Iterator over each of the test parameters. A consumer of that method will be triggered once time for each set of parameters from the provider. Parameters are injected into the consumer method at execution time.<br \/>\nThis greatly eases testing various expectations that run through essentially the same path of execution. <\/p>\n<p>Here&#8217;s the simple method under test, courtesy of <a href=\"http:\/\/groovy-almanac.org\/the-inject-method-of-list\/\">this example on Groovy Almanac<\/a><\/p>\n<pre class=\"brush: groovy; title: ; notranslate\" title=\"\">\r\n    \/**\r\n     * Use the Groovy added List.inject() method to sum a list of numbers.\r\n     *\/\r\n    def sum(list)\r\n    {\r\n        def sum = list.inject(0) { sum, item -&gt; sum + item } \r\n    }\r\n<\/pre>\n<p>And here&#8217;s the corresponding DataProvider test harness. The test is injected with a List of numbers to sum and the associated total expected for each case.<\/p>\n<pre class=\"brush: groovy; title: ; notranslate\" title=\"\">\r\n    @DataProvider (name = &quot;test1&quot;)\r\n    public Object&#x5B;]&#x5B;] createListInjectSumData() {\r\n        def array = new Object&#x5B;3]&#x5B;]\r\n        array&#x5B;0] = &#x5B;&#x5B;1, 2, 3], 6] as Object&#x5B;]\r\n        array&#x5B;1] = &#x5B;&#x5B;2, 4, 6], 12] as Object&#x5B;]\r\n        array&#x5B;2] = &#x5B;&#x5B;3, 6, 9], 18] as Object&#x5B;]\r\n        return array\r\n    }\r\n\r\n    @Test (dataProvider = &quot;test1&quot;)\r\n    void testListInjectSummation(list, expectedSum) {\r\n        Assert.assertEquals(new ListInjectExample().sum(list), expectedSum)\r\n    }\r\n<\/pre>\n<p>I&#8217;ve put the maven project I used to demo this up on github <a href=\"git:\/\/github.com\/kellyrob99\/TestNG-DataProvider-Demo.git\">here<\/a>. Mostly just because I&#8217;m having a great time using <a class=\"zem_slink\" href=\"http:\/\/git-scm.com\/\" title=\"Git (software)\" rel=\"homepage\">Git<\/a> lately.<\/p>\n<p> \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TestNG is a great tool for testing in Java, and it works even better with a little Groovy thrown in. Just lately I&#8217;ve had a lot of success using the DataProvider pattern. A DataProvider method in TestNG can return either a two dimensional Object array or an Iterator over each of the test parameters. A [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[6],"tags":[114,262,116,257,258,42,35,49],"class_list":["post-590","post","type-post","status-publish","format-standard","hentry","category-dev","tag-dataprovider","tag-git","tag-github","tag-groovy","tag-java","tag-maven","tag-programming","tag-testng"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/prjtg-9w","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.kellyrob99.com\/blog\/wp-json\/wp\/v2\/posts\/590","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kellyrob99.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kellyrob99.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kellyrob99.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kellyrob99.com\/blog\/wp-json\/wp\/v2\/comments?post=590"}],"version-history":[{"count":15,"href":"https:\/\/www.kellyrob99.com\/blog\/wp-json\/wp\/v2\/posts\/590\/revisions"}],"predecessor-version":[{"id":605,"href":"https:\/\/www.kellyrob99.com\/blog\/wp-json\/wp\/v2\/posts\/590\/revisions\/605"}],"wp:attachment":[{"href":"https:\/\/www.kellyrob99.com\/blog\/wp-json\/wp\/v2\/media?parent=590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kellyrob99.com\/blog\/wp-json\/wp\/v2\/categories?post=590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kellyrob99.com\/blog\/wp-json\/wp\/v2\/tags?post=590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}