Reading messages from message bean in functional tests

27 / Apr / 2010 by Vishal Sahu 2 comments

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 for me.

 
    // to get the message source bean
    def messageSource = ConfigurationHolder.config.applicationContext.getBean("messageSource")   
    Locale locale = new Locale('en', 'US')
    Object[]  ARGS_EMPTY = [].toArray()       // empty array -- when we don't want to pass any arguments to message
    Object[]  ARGS_WITH_VALUES = [].toArray()  // array of arguments


// method to get message from message bean

String getMessage(String key, def targetArgs = ARGS_EMPTY) {
        def keyValue = messageSource.resolveCode(key, locale)
        return keyValue?.format(targetArgs)
    }

So wherever i needed the message for assertion , i simply called the method getMessage(), which i have defined above.

eg:-


// message with arguments
 ARGS_WITH_VALUES = ["${value1}","${value2}"]
 assertContentContains getMessage('default.create.message', ARGS_WITH_VALUES)
         
// message withour arguments
 assertContentContains getMessage('default.create.message')

Hope you find it useful.


Regards
Vishal
vishal [at] intelligrape.com

Tag -

FOUND THIS USEFUL? SHARE IT

comments (2)

  1. 高性价比网购商品推荐

    This is a very good tip particularly to those new to the blogosphere.
    Short but very accurate information… Thanks for sharing this one.

    A must read article!

    Reply

Leave a comment -