Grails - 如何在integration.groovy中集成test中注入bean

时间:2014-04-17 14:00:21

标签: grails integration-testing

我在resources.groovy中定义了1个bean

cachedbean(serviceImpl) {
  }

在服务中我这样使用它

    MyService{

      static transactional = false
      def cachedbean

        myMeth(){
         cachedbean.get("cacheKey")
      }
   }

这很好用,但是当我尝试使用集成测试对其进行测试时,我会在&{39; nullpointer'上获得get例外。 cachedbean get(" cacheKey&#34)。

它是如何运作的?

2 个答案:

答案 0 :(得分:0)

如果让Grails自动将服务bean连接到集成测试类,而不是“新” - 启动服务实例,它应该是自动连接的。

class MyServiceTests extends GroovyTestCase {
    def myService
    void testSomething () {
        // myService should already be wired up
    }
}

答案 1 :(得分:-1)

  1. 目前还不清楚你是否根据你的示例代码做了,但是你需要在resources.groovy中完全限定“serviceImpl”的包和类名,除非你明确导入包。

  2. 您可能需要在resources.groovybean.autowire = "byName"

  3. 中添加声明范围
相关问题