How to unit or integration test use of injected messageSource for i18n in Grails 2.0 service

后端 未结 3 458
暖寄归人
暖寄归人 2020-12-16 00:00

I make use of a message bundle in one of my services in a Grails 2.0 project for internationalized text. The use case is an email subject that is sent via the mail plugin in

3条回答
  •  清歌不尽
    2020-12-16 00:02

    In unit tests and the local side of functional tests, sometimes you want the real properties that are in the 18n directory.

    This works for me:

      MessageSource getI18n() {
        // assuming the test cwd is the project dir (where application.properties is)
        URL url = new File('grails-app/i18n').toURI().toURL()
        def messageSource = new ResourceBundleMessageSource()
        messageSource.bundleClassLoader = new URLClassLoader(url)
        messageSource.basename = 'messages'
        messageSource
      }
    
      i18n.getMessage(key, params, locale)
    

提交回复
热议问题