{"id":600,"date":"2010-04-27T13:01:11","date_gmt":"2010-04-27T07:31:11","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=600"},"modified":"2010-06-09T15:28:24","modified_gmt":"2010-06-09T09:58:24","slug":"reading-messages-from-message-bean-in-functional-tests","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/reading-messages-from-message-bean-in-functional-tests\/","title":{"rendered":"Reading messages from message bean in functional tests"},"content":{"rendered":"<p>Hi,<br \/>\nIn one of my project i need to read message from message bundle in functional test cases. Such that whenever the message or label changes, we can just change it in message.properties and hence avoiding the test to fail.<\/p>\n<p>To achieve so, i wrote the following code in my test file and it worked for me.<\/p>\n<pre lang=\"groovy\"> \r\n    \/\/ to get the message source bean\r\n    def messageSource = ConfigurationHolder.config.applicationContext.getBean(\"messageSource\")   \r\n    Locale locale = new Locale('en', 'US')\r\n    Object[]  ARGS_EMPTY = [].toArray()       \/\/ empty array -- when we don't want to pass any arguments to message\r\n    Object[]  ARGS_WITH_VALUES = [].toArray()  \/\/ array of arguments\r\n\r\n\r\n\/\/ method to get message from message bean\r\n\r\nString getMessage(String key, def targetArgs = ARGS_EMPTY) {\r\n        def keyValue = messageSource.resolveCode(key, locale)\r\n        return keyValue?.format(targetArgs)\r\n    }\r\n\r\n<\/pre>\n<p>So wherever i needed the message for assertion , i simply called the method getMessage(), which i have defined above.<\/p>\n<p>eg:-<\/p>\n<pre lang=\"groovy\">\r\n\r\n\/\/ message with arguments\r\n ARGS_WITH_VALUES = [\"${value1}\",\"${value2}\"]\r\n assertContentContains getMessage('default.create.message', ARGS_WITH_VALUES)\r\n         \r\n\/\/ message withour arguments\r\n assertContentContains getMessage('default.create.message')\r\n\r\n<\/pre>\n<p>Hope you find it useful.<\/p>\n<p>&#8212;<br \/>\nRegards<br \/>\nVishal<br \/>\nvishal [at] intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, In one of my project i need to read message from message bundle in functional test cases. Such that whenever the message or label changes, we can just change it in message.properties and hence avoiding the test to fail. To achieve so, i wrote the following code in my test file and it worked [&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":2},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/600"}],"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=600"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/600\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}