【问题标题】:accessing Spring beans from a Geb test从 Geb 测试访问 Spring bean
【发布时间】:2014-05-20 17:52:13
【问题描述】:

我正在使用 Geb 为 Grails 应用程序编写一些功能测试。有没有办法从此类测试中访问 Spring bean。我尝试了明显的:

import geb.junit4.GebReportingTest

class AuthenticatedUserTests extends GebReportingTest {

    UserService userService

    @Test
    void shouldCreateNewIdea() {

        // throws NullPointerException
        userService.doSomething()
    }
}

但它不起作用。一些可能相关的附加细节:

  • 我使用的是 Grails 2.3.7
  • 我已禁用分叉
  • 我正在使用 grails test-app functional: 运行测试,即没有 -war 选项

【问题讨论】:

    标签: grails selenium functional-testing geb


    【解决方案1】:

    运行功能测试时,正在运行的应用程序运行在与测试完全不同的 JVM 中,因此您不能简单地访问正在运行的应用程序的 bean,因为它们不在同一个 JVM 中。这个问题有多种解决方案,您可以:

    a) 测试调用服务的控制器操作(即通过 HTTP 公开的东西)

    b) 如果服务只是填充数据,您可以使用固定装置插件为您填充数据 (http://grails.org/plugin/fixtures) 或仅在测试环境中填充 BootStrap 中的数据

    c) 使用像remote-control (http://grails.org/plugin/remote-control) 这样的插件来执行一些在服务器上运行的代码来做你想做的事情

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多