{"id":17897,"date":"2015-03-22T18:36:27","date_gmt":"2015-03-22T13:06:27","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=17897"},"modified":"2015-10-16T15:32:07","modified_gmt":"2015-10-16T10:02:07","slug":"unit-test-a-method-returning-an-object-of-protected-class","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/unit-test-a-method-returning-an-object-of-protected-class\/","title":{"rendered":"Unit test a method returning an object of protected class"},"content":{"rendered":"<p>Following is an example of testing a method which returns an object of protect class:<\/p>\n<p>In the <a title=\"Fetching list of message codes from message.properties file\" href=\"http:\/\/www.tothenew.com\/blog\/fetching-list-of-message-codes-from-message-properties-file\">previous blog<\/a> we talked about fetching the list of message codes from message.properties file.<\/p>\n<p>While writing the test case for <strong>CustomisedPluginAwareResourceBundleMessageSource<\/strong>, I was not able to test the message properties that are available when your application is actually running. Functionality rendered by getMergedProperties() method of ReloadableResourceBundleMessageSource.java is not available while writing test cases.<\/p>\n<p>Signature for getMergedProperties() is<\/p>\n<p>[java]<br \/>\nprotected PropertiesHolder getMergedProperties(final Locale locale){}<br \/>\n[\/java]<\/p>\n<p>Also the class <strong>PropertiesHolder<\/strong> is protected so we cant access it in our test files.<\/p>\n<p>Here is a proposed solution for testing listMessageCodes(). Create a class like <strong>CustomProperties<\/strong> which is similar to <strong>PropertiesHolder<\/strong> in behaviour:<\/p>\n<p>[java]<br \/>\nclass CustomProperties {<br \/>\n    Properties properties<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Mock getMergedProperties():<\/p>\n<p>[java]<br \/>\nmessageSource.metaClass.getMergedProperties = { final Locale tempLocale -&gt;<br \/>\n            Properties properties = new Properties()<br \/>\n            properties.setProperty(&#8216;square.black.label&#8217;, &#8216;Black Square&#8217;)<br \/>\n            properties.setProperty(&#8216;circle.violet.label&#8217;, &#8216;Violet Circle&#8217;)<br \/>\n            properties.setProperty(&#8216;fruit.black.label&#8217;, &#8216;Black Grape&#8217;)<\/p>\n<p>            CustomProperties customProperties = new CustomProperties()<br \/>\n            customProperties.properties = properties<br \/>\n            return customProperties<br \/>\n        }<\/p>\n<p>\/\/messageSource referred here is<br \/>\n\/\/   CustomisedPluginAwareResourceBundleMessageSource messageSource<br \/>\n[\/java]<\/p>\n<p>Finally, our test case will look like<\/p>\n<p>[java]<br \/>\nvoid &quot;test listMessageCodes&quot;() {<br \/>\n        setup:<br \/>\n        Locale locale = new Locale(&#8216;en&#8217;, &#8216;US&#8217;)<br \/>\n        String lookupMessageCode = &#8216;black&#8217;<br \/>\n        messageSource.metaClass.getMergedProperties = { final Locale tempLocale -&gt;<br \/>\n            Properties properties = new Properties()<br \/>\n            properties.setProperty(&#8216;square.black.label&#8217;, &#8216;Black Square&#8217;)<br \/>\n            properties.setProperty(&#8216;circle.violet.label&#8217;, &#8216;Violet Circle&#8217;)<br \/>\n            properties.setProperty(&#8216;fruit.black.label&#8217;, &#8216;Black Grape&#8217;)<\/p>\n<p>            CustomProperties customProperties = new CustomProperties()<br \/>\n            customProperties.properties = properties<br \/>\n            return customProperties<br \/>\n        }<\/p>\n<p>        when:<br \/>\n        List list = messageSource.listMessageCodes(locale, lookupMessageCode)<\/p>\n<p>        then:<br \/>\n        list.get(0).toString().contains(&#8216;black&#8217;)<br \/>\n    }<br \/>\n[\/java]<\/p>\n<p>Don&#8217;t forget to add <strong>@ConfineMetaClassChanges<\/strong> to the spec. It restores the meta classes to its previous state.<\/p>\n<p>That&#8217;s it&#8230;<\/p>\n<p>Search for a demo <a title=\"demo\" href=\"https:\/\/github.com\/neha-gupta11\/filtering-i18N-message-codes-properties\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Following is an example of testing a method which returns an object of protect class: In the previous blog we talked about fetching the list of message codes from message.properties file. While writing the test case for CustomisedPluginAwareResourceBundleMessageSource, I was not able to test the message properties that are available when your application is actually [&hellip;]<\/p>\n","protected":false},"author":115,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[1],"tags":[4840,492,1677,1678,319,1159],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17897"}],"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\/115"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=17897"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17897\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=17897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=17897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=17897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}