2

该线程描述了如何在视图中调用服务:如何以编程方式获取 Grails 服务的实例?

这描述了如何在 Servlet 中调用它:如何以编程方式获取 Grails 服务的实例?

这一篇说如何在 Controller 中调用它:How to dynamic select a service in Grails

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

4

2 回答 2

8

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

def someService
于 2013-08-23T13:58:20.970 回答
6

看看Testing Controllers with Service

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

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

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

于 2013-08-23T13:58:44.707 回答