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

后端 未结 3 453
暖寄归人
暖寄归人 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:07

    In a unit test you could ensure that you're wired up correctly by doing something like this:

    void testSubjectsDefaultLocale() {
        def messageSource = new Object()
        messageSource.metaClass.getMessage = {subject, params, locale ->
            assert "my.email.subject" == subject
            assert ["Passed1", "Passed2"] == params 
            assert Locale.ENGLISH == locale
            "It Worked!!!"
        }
        service.messageSource = messageSource
        String actual = service.getEmailSubjectForStandardMustGiveGiftFromBusiness(Locale.ENGLISH, Passed1 Passed2)
        assert "It Worked!!!" == actual
    }
    

    This will help ensure that you're wired up correctly but it will not ensure that what you're doing actually works. If you're comfortable with that then this would work for you. If you're trying to test that when you give "XYZ" to your .properties file it returns "Hello" then this will not work for you.

提交回复
热议问题