1
i have tagLib like this:

静态命名空间 = 'j2'

  def brandMessage = { attrs, body ->   
    def brand = ConfigurationHolder.config.j2.brand
    def messageCode = (brand ? brand+".":"") + attrs.code    
    def defaultCode = attrs.default ?: attrs.code
    out << message(code:messageCode,      default:message(code:attrs.code,default:defaultCode))
  }

并使用:

public static def translate = {code,Object[] args->
      def dflt = (args.size()>0?args[0]:"");
        def taglib = ApplicationHolder.application?.getMainContext()?.getBean("com.j2.taglib.BrandMessageTagLib")
        def translatedValue = taglib?.brandMessage(code:code,default:dflt)?:""

        return translatedValue
      }

在我们使用的控制器中:

flash.message = I18nUtils.translate('admin.groups.delete.success')

在运行应用程序时它工作正常。但是当我们为控制器创建单元测试用例时,它会抛出如下错误消息:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'com.j2.taglib.BrandMessageTagLib' is defined

但我在 Test 类中这样定义:

defineBeans {
                myService(BrandMessageTagLib)
            }
            def beanName=applicationContext.getBean("myService")

            log.info("beanName->"+beanName)

            def mockMyTagLib = mockFor(BrandMessageTagLib)
            mockMyTagLib.demand.brandMessage(1) {code, defaultMessage ->
                def defCode = new MarkupBuilder(new StringWriter())
                defCode.applyTemplate('<j2:brandMessage code="admin.groups.delete.success"')
            }
            controller.brandMessageTagLib = mockMyTagLib.createMock()

即使这也是相同的错误消息抛出。

任何有想法的人都会很有帮助。

谢谢阿纳根德拉

4

0 回答 0