{"id":1056,"date":"2010-06-14T15:24:45","date_gmt":"2010-06-14T09:54:45","guid":{"rendered":"http:\/\/www.tothenew.com\/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:\/\/www.tothenew.com\/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":5},"categories":[7],"tags":[298,273,272],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1056"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1056"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1056\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}