{"id":3865,"date":"2011-06-14T13:37:30","date_gmt":"2011-06-14T08:07:30","guid":{"rendered":"https:\/\/2thenew.today\/blog\/?p=3865"},"modified":"2011-06-14T13:37:30","modified_gmt":"2011-06-14T08:07:30","slug":"initializations-in-grails-unit-test-cases","status":"publish","type":"post","link":"https:\/\/2thenew.today\/blog\/initializations-in-grails-unit-test-cases\/","title":{"rendered":"Initializations in Grails Unit Test Cases"},"content":{"rendered":"<p> Most of us have experienced how important the Unit test cases are for our code base. I feel the other thing that is equally important, is the speed at which we can write the unit test cases. In Unit Testing, initializing different objects in setup of the test case (e.g., while testing a method which queries the database) can become a pain, especially when the objects to be initialized have many attributes and relationships. Grails\/Spock have simplified initialization process for us. All we need to is to initialize just the required attributes of the objects, along with the <b>id<\/b> field,e.g., while testing a method of a class Student : <\/p>\n<p>[java]<br \/>\nclass Student {<br \/>\n  String name<br \/>\n  String fathersName<br \/>\n  String address<br \/>\n  static belongsTo = [school: School]<\/p>\n<p> static constraints = {<br \/>\n    name(nullable: false)<br \/>\n    fathersName(nullable: false)<br \/>\n    address(nullable: false)<br \/>\n  }<\/p>\n<p>  \/\/ METHOD TO BE TESTED<br \/>\n  List&lt;Student&gt; findMySchoolsStudentsWithNamesLike(String nameLike) {<br \/>\n    List&lt;Student&gt; students = Student.findAllBySchoolAndNameLike(this.school, &#8216;%&#8217; + nameLike + &#8216;%&#8217;)<br \/>\n    return students<br \/>\n  }<\/p>\n<p>}<\/p>\n<p>[\/java]<\/p>\n<p> To test the method, a Spock test case with the simplified initializations will be like:   <\/p>\n<p>[java]<br \/>\n def &quot;My School students are retrieved correctly&quot;() {<br \/>\n    setup:<br \/>\n    School school = new School(id: 1)<br \/>\n    List&lt;Student&gt; students = (1..10).collect {new Student(id: it, name: &quot;name${it}&quot;, school: school)}<br \/>\n    mockDomain(Student, students)<\/p>\n<p>    expect:<br \/>\n    students[0].findMySchoolsStudentsWithNamesLike(&#8216;1&#8217;).size() == 2<br \/>\n  }<br \/>\n[\/java]<\/p>\n<p> Similar setup can work in Grail Unit test case.<br \/>\n Simplification of the initialization process has made Unit testing simpler and faster, thus allowing us to write more Unit test cases.<\/p>\n<p>Cheers,<br \/>\nImran Mir<br \/>\nimran@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most of us have experienced how important the Unit test cases are for our code base. I feel the other thing that is equally important, is the speed at which we can write the unit test cases. In Unit Testing, initializing different objects in setup of the test case (e.g., while testing a method which [&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":12,"footnotes":""},"categories":[7],"tags":[318,581,580,582,583],"class_list":["post-3865","post","type-post","status-publish","format-standard","hentry","category-grails","tag-grails-unit-test","tag-grails-unit-test-case-setup-initialization","tag-grails-unit-test-initialization","tag-spock-setup","tag-spock-unit-test-setup"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"Most of us have experienced how important the Unit test cases are for our code base. I feel the other thing that is equally important, is the speed at which we can write the unit test cases. In Unit Testing, initializing different objects in setup of the test case (e.g., while testing a method which\" \/>\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.today\/blog\/initializations-in-grails-unit-test-cases\/\" \/>\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=\"Initializations in Grails Unit Test Cases | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"Most of us have experienced how important the Unit test cases are for our code base. I feel the other thing that is equally important, is the speed at which we can write the unit test cases. In Unit Testing, initializing different objects in setup of the test case (e.g., while testing a method which\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/2thenew.today\/blog\/initializations-in-grails-unit-test-cases\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/2thenew.today\/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=\"Initializations in Grails Unit Test Cases | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Most of us have experienced how important the Unit test cases are for our code base. I feel the other thing that is equally important, is the speed at which we can write the unit test cases. In Unit Testing, initializing different objects in setup of the test case (e.g., while testing a method which\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/2thenew.today\/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\\\/initializations-in-grails-unit-test-cases\\\/#article\",\"name\":\"Initializations in Grails Unit Test Cases | TO THE NEW Blog\",\"headline\":\"Initializations in Grails Unit Test Cases\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/imran\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2011-06-14T13:37:30+05:30\",\"dateModified\":\"2011-06-14T13:37:30+05:30\",\"inLanguage\":\"en-US\",\"commentCount\":4,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/initializations-in-grails-unit-test-cases\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/initializations-in-grails-unit-test-cases\\\/#webpage\"},\"articleSection\":\"Grails, grails unit test, grails unit test case setup initialization, grails unit test initialization, Spock setup, Spock Unit test setup\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/initializations-in-grails-unit-test-cases\\\/#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\\\/initializations-in-grails-unit-test-cases\\\/#listItem\",\"name\":\"Initializations in Grails Unit Test Cases\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/initializations-in-grails-unit-test-cases\\\/#listItem\",\"position\":3,\"name\":\"Initializations in Grails Unit Test Cases\",\"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\\\/initializations-in-grails-unit-test-cases\\\/#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\\\/initializations-in-grails-unit-test-cases\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/initializations-in-grails-unit-test-cases\\\/\",\"name\":\"Initializations in Grails Unit Test Cases | TO THE NEW Blog\",\"description\":\"Most of us have experienced how important the Unit test cases are for our code base. I feel the other thing that is equally important, is the speed at which we can write the unit test cases. In Unit Testing, initializing different objects in setup of the test case (e.g., while testing a method which\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/initializations-in-grails-unit-test-cases\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/imran\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/imran\\\/#author\"},\"datePublished\":\"2011-06-14T13:37:30+05:30\",\"dateModified\":\"2011-06-14T13:37: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":"Initializations in Grails Unit Test Cases | TO THE NEW Blog","description":"Most of us have experienced how important the Unit test cases are for our code base. I feel the other thing that is equally important, is the speed at which we can write the unit test cases. In Unit Testing, initializing different objects in setup of the test case (e.g., while testing a method which","canonical_url":"https:\/\/2thenew.today\/blog\/initializations-in-grails-unit-test-cases\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/2thenew.today\/blog\/initializations-in-grails-unit-test-cases\/#article","name":"Initializations in Grails Unit Test Cases | TO THE NEW Blog","headline":"Initializations in Grails Unit Test Cases","author":{"@id":"https:\/\/2thenew.today\/blog\/author\/imran\/#author"},"publisher":{"@id":"https:\/\/2thenew.today\/blog\/#organization"},"datePublished":"2011-06-14T13:37:30+05:30","dateModified":"2011-06-14T13:37:30+05:30","inLanguage":"en-US","commentCount":4,"mainEntityOfPage":{"@id":"https:\/\/2thenew.today\/blog\/initializations-in-grails-unit-test-cases\/#webpage"},"isPartOf":{"@id":"https:\/\/2thenew.today\/blog\/initializations-in-grails-unit-test-cases\/#webpage"},"articleSection":"Grails, grails unit test, grails unit test case setup initialization, grails unit test initialization, Spock setup, Spock Unit test setup"},{"@type":"BreadcrumbList","@id":"https:\/\/2thenew.today\/blog\/initializations-in-grails-unit-test-cases\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog#listItem","position":1,"name":"Home","item":"https:\/\/2thenew.today\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/category\/grails\/#listItem","name":"Grails"}},{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/category\/grails\/#listItem","position":2,"name":"Grails","item":"https:\/\/2thenew.today\/blog\/category\/grails\/","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/initializations-in-grails-unit-test-cases\/#listItem","name":"Initializations in Grails Unit Test Cases"},"previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/initializations-in-grails-unit-test-cases\/#listItem","position":3,"name":"Initializations in Grails Unit Test Cases","previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/category\/grails\/#listItem","name":"Grails"}}]},{"@type":"Organization","@id":"https:\/\/2thenew.today\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/2thenew.today\/blog\/"},{"@type":"Person","@id":"https:\/\/2thenew.today\/blog\/author\/imran\/#author","url":"https:\/\/2thenew.today\/blog\/author\/imran\/","name":"Imran Mir","image":{"@type":"ImageObject","@id":"https:\/\/2thenew.today\/blog\/initializations-in-grails-unit-test-cases\/#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.today\/blog\/initializations-in-grails-unit-test-cases\/#webpage","url":"https:\/\/2thenew.today\/blog\/initializations-in-grails-unit-test-cases\/","name":"Initializations in Grails Unit Test Cases | TO THE NEW Blog","description":"Most of us have experienced how important the Unit test cases are for our code base. I feel the other thing that is equally important, is the speed at which we can write the unit test cases. In Unit Testing, initializing different objects in setup of the test case (e.g., while testing a method which","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/2thenew.today\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/2thenew.today\/blog\/initializations-in-grails-unit-test-cases\/#breadcrumblist"},"author":{"@id":"https:\/\/2thenew.today\/blog\/author\/imran\/#author"},"creator":{"@id":"https:\/\/2thenew.today\/blog\/author\/imran\/#author"},"datePublished":"2011-06-14T13:37:30+05:30","dateModified":"2011-06-14T13:37:30+05:30"},{"@type":"WebSite","@id":"https:\/\/2thenew.today\/blog\/#website","url":"https:\/\/2thenew.today\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/2thenew.today\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Initializations in Grails Unit Test Cases | TO THE NEW Blog","og:description":"Most of us have experienced how important the Unit test cases are for our code base. I feel the other thing that is equally important, is the speed at which we can write the unit test cases. In Unit Testing, initializing different objects in setup of the test case (e.g., while testing a method which","og:url":"https:\/\/2thenew.today\/blog\/initializations-in-grails-unit-test-cases\/","og:image":"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Initializations in Grails Unit Test Cases | TO THE NEW Blog","twitter:description":"Most of us have experienced how important the Unit test cases are for our code base. I feel the other thing that is equally important, is the speed at which we can write the unit test cases. In Unit Testing, initializing different objects in setup of the test case (e.g., while testing a method which","twitter:image":"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"3865","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-29 12:13:55","updated":"2024-02-29 08:25: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.today\/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.today\/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\tInitializations in Grails Unit Test Cases\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/2thenew.today\/blog"},{"label":"Grails","link":"https:\/\/2thenew.today\/blog\/category\/grails\/"},{"label":"Initializations in Grails Unit Test Cases","link":"https:\/\/2thenew.today\/blog\/initializations-in-grails-unit-test-cases\/"}],"_links":{"self":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/posts\/3865","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/comments?post=3865"}],"version-history":[{"count":0,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/posts\/3865\/revisions"}],"wp:attachment":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/media?parent=3865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/categories?post=3865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/tags?post=3865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}