2013-01-16 22 views
1

使用Spock(0.7)Grails(2.1.2)插件,您可以编写自动注入Grails原型(如服务)的集成规范。不过,我想为Spring beans做同样的事情,只在我的resources.groovy中声明。例如:如何自动将resources.groovy中声明的Grails组件注入到我的IntegrationSpecs中?

beans = { 
    simpleBean(SimpleBean) { 
     // load some dependencies 
    } 
} 

其中SimpleBean在src/groovy文件夹中声明。如果这是一个Grails服务,我可以写像下面这样在我test/integration文件夹::

import grails.plugin.spock.IntegrationSpec 

class SimpleBeanSpec extends IntegrationSpec { 

    def simpleBean 

    def "should"() { 
     when: 
      data = simpleBean.load() 
     then: 
      assert simpleBean 
    } 

} 

但上面的呼叫simpleBean.load()抛出一个NullPointerException。有什么方法可以让Spock/Grails创建simpleBean依赖关系,以便像Grails服务一样从resources.groovy获得所有已配置的依赖关系?

回答

0

Grails会自动将所有bean注入到集成测试中,包括Spock测试。在resources.conf中声明的豆类应与文物相同。您的IntegrationSpecs是否位于test/integration下?

+0

测试是在测试/集成,我会更新问题来反映,但这绝对不适用于我的安装Grails。 –

相关问题