{"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":10,"footnotes":""},"categories":[7],"tags":[],"class_list":["post-3145","post","type-post","status-publish","format-standard","hentry","category-grails"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"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 =\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Salil\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.tothenew.com\/blog\/load-codecs-in-unit-tests-using-groovy-grails\/\" \/>\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=\"Load codecs in Unit tests using Groovy Grails | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"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 =\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.tothenew.com\/blog\/load-codecs-in-unit-tests-using-groovy-grails\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/www.tothenew.com\/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=\"Load codecs in Unit tests using Groovy Grails | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"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 =\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/www.tothenew.com\/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\\\/load-codecs-in-unit-tests-using-groovy-grails\\\/#article\",\"name\":\"Load codecs in Unit tests using Groovy Grails | TO THE NEW Blog\",\"headline\":\"Load codecs in Unit tests using Groovy Grails\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/salil\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2011-02-23T13:30:19+05:30\",\"dateModified\":\"2016-12-19T15:30:06+05:30\",\"inLanguage\":\"en-US\",\"commentCount\":6,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/load-codecs-in-unit-tests-using-groovy-grails\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/load-codecs-in-unit-tests-using-groovy-grails\\\/#webpage\"},\"articleSection\":\"Grails\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/load-codecs-in-unit-tests-using-groovy-grails\\\/#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\\\/load-codecs-in-unit-tests-using-groovy-grails\\\/#listItem\",\"name\":\"Load codecs in Unit tests using Groovy Grails\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/load-codecs-in-unit-tests-using-groovy-grails\\\/#listItem\",\"position\":3,\"name\":\"Load codecs in Unit tests using Groovy Grails\",\"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\\\/salil\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/salil\\\/\",\"name\":\"Salil\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/load-codecs-in-unit-tests-using-groovy-grails\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/97e216cba95b296d5909ad7b93d1c338d0fdad7b95410c9412b126a90f4735d9?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Salil\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/load-codecs-in-unit-tests-using-groovy-grails\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/load-codecs-in-unit-tests-using-groovy-grails\\\/\",\"name\":\"Load codecs in Unit tests using Groovy Grails | TO THE NEW Blog\",\"description\":\"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 = \\\"http:\\\/\\\/example.com\\\/service\\\" String username = \\\"usernameStr\\\" String password = \\\"passwordvalue\\\" String urlStr =\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/load-codecs-in-unit-tests-using-groovy-grails\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/salil\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/salil\\\/#author\"},\"datePublished\":\"2011-02-23T13:30:19+05:30\",\"dateModified\":\"2016-12-19T15:30:06+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":"Load codecs in Unit tests using Groovy Grails | TO THE NEW Blog","description":"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 = \"http:\/\/example.com\/service\" String username = \"usernameStr\" String password = \"passwordvalue\" String urlStr =","canonical_url":"https:\/\/www.tothenew.com\/blog\/load-codecs-in-unit-tests-using-groovy-grails\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.tothenew.com\/blog\/load-codecs-in-unit-tests-using-groovy-grails\/#article","name":"Load codecs in Unit tests using Groovy Grails | TO THE NEW Blog","headline":"Load codecs in Unit tests using Groovy Grails","author":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/salil\/#author"},"publisher":{"@id":"https:\/\/www.tothenew.com\/blog\/#organization"},"datePublished":"2011-02-23T13:30:19+05:30","dateModified":"2016-12-19T15:30:06+05:30","inLanguage":"en-US","commentCount":6,"mainEntityOfPage":{"@id":"https:\/\/www.tothenew.com\/blog\/load-codecs-in-unit-tests-using-groovy-grails\/#webpage"},"isPartOf":{"@id":"https:\/\/www.tothenew.com\/blog\/load-codecs-in-unit-tests-using-groovy-grails\/#webpage"},"articleSection":"Grails"},{"@type":"BreadcrumbList","@id":"https:\/\/www.tothenew.com\/blog\/load-codecs-in-unit-tests-using-groovy-grails\/#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\/load-codecs-in-unit-tests-using-groovy-grails\/#listItem","name":"Load codecs in Unit tests using Groovy Grails"},"previousItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/load-codecs-in-unit-tests-using-groovy-grails\/#listItem","position":3,"name":"Load codecs in Unit tests using Groovy Grails","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\/salil\/#author","url":"https:\/\/www.tothenew.com\/blog\/author\/salil\/","name":"Salil","image":{"@type":"ImageObject","@id":"https:\/\/www.tothenew.com\/blog\/load-codecs-in-unit-tests-using-groovy-grails\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/97e216cba95b296d5909ad7b93d1c338d0fdad7b95410c9412b126a90f4735d9?s=96&d=mm&r=g","width":96,"height":96,"caption":"Salil"}},{"@type":"WebPage","@id":"https:\/\/www.tothenew.com\/blog\/load-codecs-in-unit-tests-using-groovy-grails\/#webpage","url":"https:\/\/www.tothenew.com\/blog\/load-codecs-in-unit-tests-using-groovy-grails\/","name":"Load codecs in Unit tests using Groovy Grails | TO THE NEW Blog","description":"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 = \"http:\/\/example.com\/service\" String username = \"usernameStr\" String password = \"passwordvalue\" String urlStr =","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/www.tothenew.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/www.tothenew.com\/blog\/load-codecs-in-unit-tests-using-groovy-grails\/#breadcrumblist"},"author":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/salil\/#author"},"creator":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/salil\/#author"},"datePublished":"2011-02-23T13:30:19+05:30","dateModified":"2016-12-19T15:30:06+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"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Load codecs in Unit tests using Groovy Grails | TO THE NEW Blog","og:description":"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 =","og:url":"https:\/\/www.tothenew.com\/blog\/load-codecs-in-unit-tests-using-groovy-grails\/","og:image":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Load codecs in Unit tests using Groovy Grails | TO THE NEW Blog","twitter:description":"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 =","twitter:image":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"3145","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-30 08:15:00","updated":"2024-02-29 08:43:08","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:\/\/www.tothenew.com\/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:\/\/www.tothenew.com\/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\tLoad codecs in Unit tests using Groovy Grails\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.tothenew.com\/blog"},{"label":"Grails","link":"https:\/\/www.tothenew.com\/blog\/category\/grails\/"},{"label":"Load codecs in Unit tests using Groovy Grails","link":"https:\/\/www.tothenew.com\/blog\/load-codecs-in-unit-tests-using-groovy-grails\/"}],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3145","targetHints":{"allow":["GET"]}}],"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}]}}