{"id":1241,"date":"2010-07-14T20:42:28","date_gmt":"2010-07-14T15:12:28","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1241"},"modified":"2010-07-16T14:22:28","modified_gmt":"2010-07-16T08:52:28","slug":"grails-unit-testing-for-beginners","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/grails-unit-testing-for-beginners\/","title":{"rendered":"Grails unit testing for beginners"},"content":{"rendered":"<p><em>Ques: How is unit testing different from integration testing ?<\/em><\/p>\n<p>Ans: Integration tests need to bring up the whole grails environment.They talk to the database. All the dynamic GORM methods and properties are available here. Unit testing are small focused, fast loading tests that do not load supporting components.<\/p>\n<p><em>Ques: How can I unit test a method in a service ?<\/em><\/p>\n<p>Ans: You would need to follow these simple steps:<\/p>\n<p>Suppose you want to write a unit test case for a someFunction() in MyService:<\/p>\n<blockquote>\n<pre lang=\"groovy\">class MyService {\r\n    public String myFunction() {\r\n      return \"testString\"\r\n    }\r\n  }<\/pre>\n<blockquote><p>-&gt; Create Unit Test File : grails create-unit-test com.intelligrape.xxx.MyService.\u00a0It will create the test file in the folder <strong>\/test\/unit<\/strong><br \/>\n-&gt; Write a testcase function<br \/>\n-&gt; Create an object of MyService<br \/>\n-&gt; Call the function and make assertions<\/p>\n<blockquote>\n<pre lang=\"groovy\">public void testMyFunction() {\r\n    def myService = new MyService()\r\n    String string = myService.myFunction()\r\n    assertEquals \"testString\", string\r\n  }<\/pre>\n<\/blockquote>\n<p><em>Ques: I am saving an object from this function. How can I test that ?<\/em><\/p>\n<blockquote>\n<pre lang=\"groovy\"> public void myFunction(int number, String name) {\r\n    MyDomainClass object = new MyDomainClass(age: number, name: name)\r\n    object.save()\r\n  }<\/pre>\n<\/blockquote>\n<p>Ans: Its pretty simple. You would first need to mock the domain class whose object is being saved.<\/p>\n<blockquote>\n<pre lang=\"groovy\">   def instances = []\r\n   def myTestDomain = mockDomain('MyDomainClass',instances)<\/pre>\n<\/blockquote>\n<p><em> instances<\/em> will serve as a cache of objects. Right now it contains no object. But when you call the save method from the function to be tested, it will automatically put that object in this cache. So, we can make assertions against this cache. Just to remind you, no database communication occurs during the unit testing.<\/p>\n<blockquote>\n<pre lang=\"groovy\"> public void testMyFunction() {\r\n    def instances = []\r\n    def myTestDomain = mockDomain('MyDomainClass', instances)\r\n    def myService = new MyService()\r\n    String string = myService.myFunction(30, 'myName')\r\n    assertEquals 1, instances.size()\r\n  }<\/pre>\n<\/blockquote>\n<p><em>Ques: What does this <strong>mockDomain<\/strong><\/em><em> do ?<\/em><\/p>\n<p>Ans: mockDomain is a method of GrailsUnitTestCase, that helps to mock a domain class. mockDomain also mocks most of the injected methods of the domain class like, save(), validate(),delete(),get() and many others. But there are many methods it does not mock,e.g., createCriteria,find,findAll,withTransaction and many others.<\/p>\n<p><em>Ques: That was great. But now my function uses some other service to do some job. How can I test that ?<\/em><\/p>\n<blockquote>\n<pre lang=\"groovy\">  public void myFunction(String empId) {\r\n    String name = otherService.someOtherFunction(empId)\r\n    MyDomainClass object = new MyDomainClass(name: name)\r\n    object.save()\r\n  }<\/pre>\n<\/blockquote>\n<p>Ans: In this case you would need to mock both, other service as well as the method which is called.<\/p>\n<blockquote>\n<pre lang=\"groovy\">void testMyFunction() {\r\n    def otherService = mockFor(OtherService)\r\n    otherService.demand.someOtherFunction() {empId-&gt; return \"testName\"}\r\n    def myService = new MyService()\r\n    myService.otherService = otherService.createMock()\r\n    String string = myService.myFunction(30, 'myName')\r\n    assertEquals 1, instances.size()\r\n}<\/pre>\n<\/blockquote>\n<p><em>Ques: What does this mockFor method do ?<\/em><\/p>\n<p>Ans: It is a method that is used to mock a dependency. We can mock services and its methods with the help of this. The <em><strong>demand<\/strong><\/em> method of the object returned by the method can be used to mock different methods of the mockedInstance. We can also specify the number of times the function is actually called in the function.<\/p>\n<blockquote>\n<pre lang=\"groovy\"> otherService.demand.someOtherFunction(1..2) {empId; return \"testName\"}<\/pre>\n<\/blockquote>\n<p>Here, it means someOtherFunction() will be called not more than 2 times in the function.<\/p>\n<p>Hope this helps.<br \/>\nImran Mir<br \/>\nimran@intelligrape.com<\/p><\/blockquote>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Ques: How is unit testing different from integration testing ? Ans: Integration tests need to bring up the whole grails environment.They talk to the database. All the dynamic GORM methods and properties are available here. Unit testing are small focused, fast loading tests that do not load supporting components. Ques: How can I unit test [&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":10},"categories":[7],"tags":[318,319],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1241"}],"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=1241"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1241\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}