{"id":21068,"date":"2015-06-16T15:33:03","date_gmt":"2015-06-16T10:03:03","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=21068"},"modified":"2015-07-13T12:49:59","modified_gmt":"2015-07-13T07:19:59","slug":"writing-test-cases-for-domain-class-using-spock","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/writing-test-cases-for-domain-class-using-spock\/","title":{"rendered":"Writing test cases for Domain class Using Spock"},"content":{"rendered":"<p>The agenda of this blog is to demonstrate how to write basic validation test cases for domain class in grails application.<\/p>\n<p>Create a domain class User with some basic constraints.<\/p>\n<p>[java]<br \/>\npackage com.ttn.testing<\/p>\n<p>class User {<br \/>\nString emailId<br \/>\nString name<\/p>\n<p>static constraints = {<br \/>\nemailId nullable: false, blank: false<br \/>\nname nullable: true, blank: false, minSize: 3, maxSize: 20<br \/>\n\t}<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Add dependencies for spock in BuildConfig.groovy<br \/>\n[java]<br \/>\ndependencies {<br \/>\ntest &quot;org.spockframework:spock-grails-support:0.7-groovy-2.0&quot;<br \/>\n}<\/p>\n<p>plugins {<br \/>\ntest(&quot;:spock:0.7&quot;) {<br \/>\nexclude &quot;spock-grails-support&quot;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>documentation of spock plugin : <a href=\"https:\/\/grails.org\/plugin\/spock\" title=\"Spock\">https:\/\/grails.org\/plugin\/spock<\/a><\/p>\n<p>1. Test case for required fields validation.<\/p>\n<p>[java]<br \/>\n@Unroll(&quot;test for required field validation&quot;)<br \/>\ndef &quot;test for required field validation&quot;() {<\/p>\n<p>when: &quot;Create user instance&quot;<br \/>\nUser user = new User(emailId: emailId)<\/p>\n<p>then: &quot;emailId should not be null&quot;<br \/>\nuser.validate() == expectedResult<\/p>\n<p>where:<br \/>\ntestDescription\t\t| emailId              | expectedResult<br \/>\n&quot;valid test&quot;        | &#8216;sanchit@sanchit.com&#8217;| true<br \/>\n&quot;null test&quot;         | null                 | false<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>2. Test case for min length validation<\/p>\n<p>[java]<br \/>\n@Unroll(&quot;min length validation&quot;)<br \/>\ndef &quot;min length validation&quot;() {<\/p>\n<p>when: &quot;Create user instance&quot;<br \/>\nUser user = new User(emailId: &quot;sanchit@sanchit.com&quot;, name: name)<\/p>\n<p>then: &quot;name minimun of 3 char&quot;<br \/>\nuser.validate() == expectation<\/p>\n<p>where:<br \/>\ntestDescription\t\t| name      | expectation<br \/>\n&quot;valid test&quot;        | &quot;sanchit&quot; | true<br \/>\n&quot;min length test&quot;   | &quot;sa&quot;      | false<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Grails automatically create a UserSpec.groovy in test\/unit folder for writing the test cases.<\/p>\n<p>Command to run test cases : grails test-app unit: User<\/p>\n<p>Above command execute the test case, generate the report and return the path of report file.<br \/>\nex-<br \/>\n<strong>Tests PASSED &#8211; view reports in<br \/>\n\/home\/sanchit\/work\/workspace\/projects\/testingWithSpock\/target\/test-reports<\/strong><\/p>\n<p>Copy the path and open it in browser to see the test report.<\/p>\n<p>In this way we can validate the domain class constraints using spock.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The agenda of this blog is to demonstrate how to write basic validation test cases for domain class in grails application. Create a domain class User with some basic constraints. [java] package com.ttn.testing class User { String emailId String name static constraints = { emailId nullable: false, blank: false name nullable: true, blank: false, minSize: [&hellip;]<\/p>\n","protected":false},"author":203,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[7,1816],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/21068"}],"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\/203"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=21068"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/21068\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=21068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=21068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=21068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}