{"id":2092,"date":"2010-11-14T19:01:01","date_gmt":"2010-11-14T13:31:01","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=2092"},"modified":"2015-05-19T17:47:13","modified_gmt":"2015-05-19T12:17:13","slug":"testing-custom-validator-in-grails-unit-test-case","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/testing-custom-validator-in-grails-unit-test-case\/","title":{"rendered":"Testing custom validator in grails unit test case"},"content":{"rendered":"<p>Hi,<\/p>\n<p>In my recent grails project, i needed to write unit test cases for testing custom validators which i created in command objects. To test validators, there is a method provided by grails &#8220;mockForConstraintsTests()&#8221; for validating constraints on Domain as well as Command-Object fields.<\/p>\n<p>Let us take a scenario where we needed to use command object for user registration form. There are various fields in the class which include fields password and confirmPassword also. Now i want to validate that the value of password and confirm password should be same before saving it to the database.<\/p>\n<p>I created custom validator for this which looks like.<\/p>\n<pre lang=\"Groovy\"> \r\n  password(nullable: false, blank: false, minSize: 6)\r\n  confirmPassword(nullable: false, blank: false, validator: {val, obj -&gt;\r\n            obj.properties['password'] == val\r\n })\r\n<\/pre>\n<p>The unit test which i write for this is like :-<\/p>\n<pre lang=\"groovy\">void testUserPasswordConstraints() {\r\n\r\n        mockForConstraintsTests(UserCommandObject)\r\n        UserCommandObject uco = new UserCommandObject()\r\n        uco.password=\"abcde\"\r\n        uco.confirmPassword=\"xyz\"\r\n        uco.validate()\r\n        assertNotNull(uco.errors[\"confirmPassword\"])  \r\n\/\/ Validation message for password and confirmPassword field do not match\r\n\r\n }\r\n<\/pre>\n<p>So, by using &#8216;mockForConstraintsTests()&#8217; method we can easily write test cases for testing constraints and custom validators for domain as well as command-object fields.It helped me a lot.<\/p>\n<p>Hope this helps.<\/p>\n<p>Vishal Sahu<br \/>\nvishal@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, In my recent grails project, i needed to write unit test cases for testing custom validators which i created in command objects. To test validators, there is a method provided by grails &#8220;mockForConstraintsTests()&#8221; for validating constraints on Domain as well as Command-Object fields. Let us take a scenario where we needed to use command [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2092"}],"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\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=2092"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2092\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=2092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=2092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=2092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}