{"id":3145,"date":"2011-02-23T13:30:19","date_gmt":"2011-02-23T08:00:19","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=3145"},"modified":"2016-12-19T15:30:06","modified_gmt":"2016-12-19T10:00:06","slug":"load-codecs-in-unit-tests-using-groovy-grails","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/load-codecs-in-unit-tests-using-groovy-grails\/","title":{"rendered":"Load codecs in Unit tests using Groovy Grails"},"content":{"rendered":"<p>This post is all about enabling codecs (like encodeAsURL, decodeURL, etc) in your Grails Unit tests. I am assuming that you are already familiar with writing unit tests.<br \/>\n<\/p>\n<p>Scenario:<br \/>\nIn our application code, if we have something like below:<br \/>\n[groovy]<br \/>\n      String serviceUrl = &quot;http:\/\/example.com\/service&quot;<br \/>\n      String username = &quot;usernameStr&quot;<br \/>\n      String password = &quot;passwordvalue&quot;<br \/>\n      String urlStr = &quot;${serviceUrl}?u=${username.encodeAsURL()}&amp;p=${password.encodeAsURL()}..&quot;<br \/>\n[\/groovy]<br \/>\nAbove code works very well when we run the application or even Integration tests. As we know that Codecs are automatically injected by Grails when application starts up.<br \/>\n<\/p>\n<p><b>Problem<\/b>:<br \/>\nWhen we use this code in our Unit Tests, it won&#8217;t work. Reason, Unit tests do not load Grails specific environment automatically. We have to mock or load the things (whatever required by test).<br \/>\n<\/p>\n<p><b>Solution<\/b>:<br \/>\nGrailsUnitTestCase class provides a method loadCodec(CodecClass). So here, to make encodeAsURL() working in our Unit tests, we just need add following line of code in our test (setup method).<br \/>\n[groovy]<br \/>\n      loadCodec(URLCodec)<br \/>\n[\/groovy]<br \/>\n<\/p>\n<p>That&#8217;s it. Rest of the work will be taken care by Grails \ud83d\ude42<\/p>\n<p>So whatever codec you need to load, just invoke loadCodec method with required codecClass in argument.<\/p>\n<p>Your comments are always Welcome. Please post if you have any!<\/p>\n<p>Cheers!<br \/>\nSalil Kalia<br \/>\nSalil [at] IntelliGrape [dot] com<br \/>\nTwitter LinkedIn <\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is all about enabling codecs (like encodeAsURL, decodeURL, etc) in your Grails Unit tests. I am assuming that you are already familiar with writing unit tests. Scenario: In our application code, if we have something like below: [groovy] String serviceUrl = &quot;http:\/\/example.com\/service&quot; String username = &quot;usernameStr&quot; String password = &quot;passwordvalue&quot; String urlStr = [&hellip;]<\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3145"}],"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\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=3145"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3145\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=3145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=3145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=3145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}