【问题标题】:How to call a Service in a Grails integration test如何在 Grails 集成测试中调用服务
【发布时间】:2013-08-23 13:52:20
【问题描述】:

这个线程描述了如何在视图中调用服务:How do I get an instance of a Grails service programmatically?

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

这个说如何在 Controller 中调用它:How to dynamically select a service in Grails

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

【问题讨论】:

  • 你不能把def myService 放在测试中,grails 应该把它注入 AFAIK

标签: grails


【解决方案1】:

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

def someService

【讨论】:

    【解决方案2】:

    看看Testing Controllers with Service

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

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

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

    【讨论】:

      猜你喜欢
      • 2013-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-07
      • 2012-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多