{"id":3865,"date":"2011-06-14T13:37:30","date_gmt":"2011-06-14T08:07:30","guid":{"rendered":"http:\/\/www.tothenew.com\/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:\/\/www.tothenew.com\/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":3},"categories":[7],"tags":[318,581,580,582,583],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3865"}],"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=3865"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3865\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=3865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=3865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=3865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}