如何在Grails集成测试中调用服务

时间:2013-08-23 13:52:21

标签: grails

此主题描述了如何在视图中调用服务:How do I get an instance of a Grails service programmatically?

这描述了如何在Servlet中调用它:How do I get an instance of a Grails service programmatically?

这个说明如何在控制器中调用它:How to dynamically select a service in Grails

我需要在集成测试中处理我的服务。这可能吗?

2 个答案:

答案 0 :(得分:8)

如果是集成测试,您可以访问整个运行时,只需像正常情况一样注入它。

def someService

答案 1 :(得分:6)

查看Testing Controllers with Service

<强>要点:
您必须在测试中将服务(spring bean)初始化为控制器。

class FilmStarsTests extends GroovyTestCase {
    def popularityService
    void testInjectedServiceInController () {
        def fsc = new FilmStarsController()
        fsc.popularityService = popularityService
        fsc.update()
    }
}

服务在集成测试中自动装配,如在控制器中一样。