{"id":1056,"date":"2010-06-14T15:24:45","date_gmt":"2010-06-14T09:54:45","guid":{"rendered":"https:\/\/2thenew.online\/blog\/?p=1056"},"modified":"2010-06-30T14:23:30","modified_gmt":"2010-06-30T08:53:30","slug":"unit-tests-getting-started-with-service-unit-test","status":"publish","type":"post","link":"https:\/\/2thenew.online\/blog\/unit-tests-getting-started-with-service-unit-test\/","title":{"rendered":"Unit-Tests : Getting started with Service Unit Test"},"content":{"rendered":"<p>Hi all,<\/p>\n<p>Here I am giving a brief introduction about  unit testing of services in grails. I will explain it with the help of a simple example.<\/p>\n<p>We have a domain class named: Item<br \/>\nTwo services : UtilService and ItemService<\/p>\n<p>Code of UtilService.groovy<\/p>\n<pre lang=\"groovy\">\/\/ class UtilService code\r\n\r\nclass UtilService {\r\n    boolean transactional = true\r\n    Integer calculatePrice(Item item, Integer vat, Integer profit) {\r\n        return item.price * vat * profit\r\n    }\r\n}\r\n<\/pre>\n<p>Code of ItemService.groovy<\/p>\n<pre lang=\"groovy\">\/\/ ItemService class code\r\n\r\nclass ItemService {\r\n    boolean transactional = true\r\n    def utilService\r\n    Integer calculateCost(Item item,Integer quantity) {\r\n        Integer price = utilService.calculatePrice(item,5,8)\r\n        Integer totalCost = price * quantity\r\n        return totalCost\r\n    }\r\n}\r\n<\/pre>\n<p>Our test case:-<\/p>\n<pre lang=\"groovy\">  void test_calculateCost() {\r\n \/\/ Step-1: Mocking the domain class\r\n        def instance = [new Item()]\r\n        mockDomain(Item, instance)\r\n\r\n\/\/Step-2: Mocking the service and its method used in the function that has to be tested\r\n        def otherService = mockFor(UtilService)\r\n        otherService.demand.calculatePrice(1) {l,m, n -&gt; return 150}\r\n\r\n\/\/ Step-3: creating the instance of our service\r\n        def itemService = new ItemService()\r\n        itemService.utilService = otherService.createMock()\r\n\r\n\/\/ Step-4: Calling the method to be tested\r\n        def amount = itemService.calculateCost(instance[0],10)\r\n\r\n        assertEquals 1500, amount\r\n    }\r\n<\/pre>\n<p>Hope it helps.<br \/>\nFor more&#8230;. wait for the next blog on unit testing<\/p>\n<p>Regards<br \/>\nImran<br \/>\nimran@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi all, Here I am giving a brief introduction about unit testing of services in grails. I will explain it with the help of a simple example. We have a domain class named: Item Two services : UtilService and ItemService Code of UtilService.groovy \/\/ class UtilService code class UtilService { boolean transactional = true Integer [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":8,"footnotes":""},"categories":[7],"tags":[298,273,272],"class_list":["post-1056","post","type-post","status-publish","format-standard","hentry","category-grails","tag-grails-testing","tag-grails-test","tag-unit-test"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"Hi all, Here I am giving a brief introduction about unit testing of services in grails. I will explain it with the help of a simple example. We have a domain class named: Item Two services : UtilService and ItemService Code of UtilService.groovy \/\/ class UtilService code class UtilService { boolean transactional = true Integer\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Imran Mir\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/2thenew.online\/blog\/unit-tests-getting-started-with-service-unit-test\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"Unit-Tests : Getting started with Service Unit Test | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"Hi all, Here I am giving a brief introduction about unit testing of services in grails. I will explain it with the help of a simple example. We have a domain class named: Item Two services : UtilService and ItemService Code of UtilService.groovy \/\/ class UtilService code class UtilService { boolean transactional = true Integer\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/2thenew.online\/blog\/unit-tests-getting-started-with-service-unit-test\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Unit-Tests : Getting started with Service Unit Test | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Hi all, Here I am giving a brief introduction about unit testing of services in grails. I will explain it with the help of a simple example. We have a domain class named: Item Two services : UtilService and ItemService Code of UtilService.groovy \/\/ class UtilService code class UtilService { boolean transactional = true Integer\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/unit-tests-getting-started-with-service-unit-test\\\/#article\",\"name\":\"Unit-Tests : Getting started with Service Unit Test | TO THE NEW Blog\",\"headline\":\"Unit-Tests : Getting started with Service Unit Test\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/imran\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2010-06-14T15:24:45+05:30\",\"dateModified\":\"2010-06-30T14:23:30+05:30\",\"inLanguage\":\"en-US\",\"commentCount\":1,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/unit-tests-getting-started-with-service-unit-test\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/unit-tests-getting-started-with-service-unit-test\\\/#webpage\"},\"articleSection\":\"Grails, grails testing, grails-test, unit-test\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/unit-tests-getting-started-with-service-unit-test\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"name\":\"Grails\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"position\":2,\"name\":\"Grails\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/unit-tests-getting-started-with-service-unit-test\\\/#listItem\",\"name\":\"Unit-Tests : Getting started with Service Unit Test\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/unit-tests-getting-started-with-service-unit-test\\\/#listItem\",\"position\":3,\"name\":\"Unit-Tests : Getting started with Service Unit Test\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"name\":\"Grails\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/imran\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/imran\\\/\",\"name\":\"Imran Mir\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/unit-tests-getting-started-with-service-unit-test\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/186fa1c29a61225f60e52068be3f954d58ffa64750eac981d45154a72275b1eb?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Imran Mir\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/unit-tests-getting-started-with-service-unit-test\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/unit-tests-getting-started-with-service-unit-test\\\/\",\"name\":\"Unit-Tests : Getting started with Service Unit Test | TO THE NEW Blog\",\"description\":\"Hi all, Here I am giving a brief introduction about unit testing of services in grails. I will explain it with the help of a simple example. We have a domain class named: Item Two services : UtilService and ItemService Code of UtilService.groovy \\\/\\\/ class UtilService code class UtilService { boolean transactional = true Integer\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/unit-tests-getting-started-with-service-unit-test\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/imran\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/imran\\\/#author\"},\"datePublished\":\"2010-06-14T15:24:45+05:30\",\"dateModified\":\"2010-06-30T14:23:30+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Unit-Tests : Getting started with Service Unit Test | TO THE NEW Blog","description":"Hi all, Here I am giving a brief introduction about unit testing of services in grails. I will explain it with the help of a simple example. We have a domain class named: Item Two services : UtilService and ItemService Code of UtilService.groovy \/\/ class UtilService code class UtilService { boolean transactional = true Integer","canonical_url":"https:\/\/2thenew.online\/blog\/unit-tests-getting-started-with-service-unit-test\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/2thenew.online\/blog\/unit-tests-getting-started-with-service-unit-test\/#article","name":"Unit-Tests : Getting started with Service Unit Test | TO THE NEW Blog","headline":"Unit-Tests : Getting started with Service Unit Test","author":{"@id":"https:\/\/2thenew.online\/blog\/author\/imran\/#author"},"publisher":{"@id":"https:\/\/2thenew.online\/blog\/#organization"},"datePublished":"2010-06-14T15:24:45+05:30","dateModified":"2010-06-30T14:23:30+05:30","inLanguage":"en-US","commentCount":1,"mainEntityOfPage":{"@id":"https:\/\/2thenew.online\/blog\/unit-tests-getting-started-with-service-unit-test\/#webpage"},"isPartOf":{"@id":"https:\/\/2thenew.online\/blog\/unit-tests-getting-started-with-service-unit-test\/#webpage"},"articleSection":"Grails, grails testing, grails-test, unit-test"},{"@type":"BreadcrumbList","@id":"https:\/\/2thenew.online\/blog\/unit-tests-getting-started-with-service-unit-test\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog#listItem","position":1,"name":"Home","item":"https:\/\/2thenew.online\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/category\/grails\/#listItem","name":"Grails"}},{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/category\/grails\/#listItem","position":2,"name":"Grails","item":"https:\/\/2thenew.online\/blog\/category\/grails\/","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/unit-tests-getting-started-with-service-unit-test\/#listItem","name":"Unit-Tests : Getting started with Service Unit Test"},"previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/unit-tests-getting-started-with-service-unit-test\/#listItem","position":3,"name":"Unit-Tests : Getting started with Service Unit Test","previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/category\/grails\/#listItem","name":"Grails"}}]},{"@type":"Organization","@id":"https:\/\/2thenew.online\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/2thenew.online\/blog\/"},{"@type":"Person","@id":"https:\/\/2thenew.online\/blog\/author\/imran\/#author","url":"https:\/\/2thenew.online\/blog\/author\/imran\/","name":"Imran Mir","image":{"@type":"ImageObject","@id":"https:\/\/2thenew.online\/blog\/unit-tests-getting-started-with-service-unit-test\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/186fa1c29a61225f60e52068be3f954d58ffa64750eac981d45154a72275b1eb?s=96&d=mm&r=g","width":96,"height":96,"caption":"Imran Mir"}},{"@type":"WebPage","@id":"https:\/\/2thenew.online\/blog\/unit-tests-getting-started-with-service-unit-test\/#webpage","url":"https:\/\/2thenew.online\/blog\/unit-tests-getting-started-with-service-unit-test\/","name":"Unit-Tests : Getting started with Service Unit Test | TO THE NEW Blog","description":"Hi all, Here I am giving a brief introduction about unit testing of services in grails. I will explain it with the help of a simple example. We have a domain class named: Item Two services : UtilService and ItemService Code of UtilService.groovy \/\/ class UtilService code class UtilService { boolean transactional = true Integer","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/2thenew.online\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/2thenew.online\/blog\/unit-tests-getting-started-with-service-unit-test\/#breadcrumblist"},"author":{"@id":"https:\/\/2thenew.online\/blog\/author\/imran\/#author"},"creator":{"@id":"https:\/\/2thenew.online\/blog\/author\/imran\/#author"},"datePublished":"2010-06-14T15:24:45+05:30","dateModified":"2010-06-30T14:23:30+05:30"},{"@type":"WebSite","@id":"https:\/\/2thenew.online\/blog\/#website","url":"https:\/\/2thenew.online\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/2thenew.online\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Unit-Tests : Getting started with Service Unit Test | TO THE NEW Blog","og:description":"Hi all, Here I am giving a brief introduction about unit testing of services in grails. I will explain it with the help of a simple example. We have a domain class named: Item Two services : UtilService and ItemService Code of UtilService.groovy \/\/ class UtilService code class UtilService { boolean transactional = true Integer","og:url":"https:\/\/2thenew.online\/blog\/unit-tests-getting-started-with-service-unit-test\/","og:image":"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Unit-Tests : Getting started with Service Unit Test | TO THE NEW Blog","twitter:description":"Hi all, Here I am giving a brief introduction about unit testing of services in grails. I will explain it with the help of a simple example. We have a domain class named: Item Two services : UtilService and ItemService Code of UtilService.groovy \/\/ class UtilService code class UtilService { boolean transactional = true Integer","twitter:image":"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"1056","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-30 08:16:02","updated":"2024-02-29 11:00:22","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.online\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.online\/blog\/category\/grails\/\" title=\"Grails\">Grails<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tUnit-Tests : Getting started with Service Unit Test\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/2thenew.online\/blog"},{"label":"Grails","link":"https:\/\/2thenew.online\/blog\/category\/grails\/"},{"label":"Unit-Tests : Getting started with Service Unit Test","link":"https:\/\/2thenew.online\/blog\/unit-tests-getting-started-with-service-unit-test\/"}],"_links":{"self":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/posts\/1056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/comments?post=1056"}],"version-history":[{"count":0,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/posts\/1056\/revisions"}],"wp:attachment":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/media?parent=1056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/categories?post=1056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/tags?post=1056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}