【问题标题】:Grails Functional Testing Plugin and RESTful TestsGrails 功能测试插件和 RESTful 测试
【发布时间】:2014-02-19 23:02:27
【问题描述】:

我刚刚获取了functional testing plugin 的最新版本,至少从文档来看,它的焦点发生了一些变化。这不是一件坏事。 HtmlUnit 集成很好。但是现在,文档中没有任何关于 Web 服务的 RESTful 测试的内容。但是,我过去对 functionaltestplugin.FunctionalTestCase 所做的事情仍然有效,这很棒。

例如:

void testCertifyEmployerCertifierNotFound() {

      post('/employerCertification/certifyEmployer') {
        headers['Content-Type'] = 'application/json'
        body {
          """
          {
            'employerName': 'ACME',
            'certifierExteralId': '1234556',
            'certifyingUserId': '123445'
          }
          """
        }
      }
      assertStatus 200
      assertContentType "application/json"
      def model = this.response.contentAsString
      def map = JSON.parse(model)
      assertFalse(map.success)
      assertNotNull(map.errorCode)
      assertTrue(map.errorCode == EmployerCertificationService.ERROR_RESPONSE.CERTIFIER_NOT_FOUND.toString())
}

这个插件仍然是用于功能 Web 服务测试的“事实上的”插件吗?我上面的方法仍然有效吗?

【问题讨论】:

    标签: grails functional-testing


    【解决方案1】:

    如果您的目标是测试 REST 请求/响应,我建议您使用 rest client builder plugin。您不需要复杂的浏览器模拟插件。安装后只需两步即可使用,非常简单:

    scripts/_Events.groovy中添加事件来管理功能测试:这是一个系统grail文件,用于在运行时挂钩一些事件。只需复制并粘贴此 sn-p:

    eventAllTestsStart = {
        if (getBinding().variables.containsKey("functionalTests")) {
            functionalTests << "functional"
        }
    } 
    

    现在您可以在 test/functional 文件夹中创建功能测试,请记住以 Spec 结尾的文件名,如果您忘记这一点,grails 将找不到任何测试。 这是一个例子:

    import grails.plugins.rest.client.RestBuilder
    import spock.lang.Specification
    
        class AuthenticationSpec extends Specification {
    
            String baseUrl = "http://localhost:8080/grouply-backend"
    
            void "test login wrong credentials"() {
    
                given: 
                RestBuilder restBuilder = new RestBuilder()
    
                when: "sending wrong credential"
                def response = restBuilder.post("${baseUrl}/auth/login") {
                json {
                    username = 'foo'
                    password = 'bar'
                    }
                }
    
                then: "authentication http error should happen"
                response.status == 401
            }
        }
    

    使用$ grails test-app functional: 运行测试

    【讨论】:

      【解决方案2】:

      我刚刚尝试将functional testing pluginwebtest plugin 与几乎最新的Grails 2.4.2 一起使用,但我很遗憾地报告它们都很无聊。 :(

      功能测试插件实际上已经被废弃了至少 9 个月。 2013 年 12 月,一个critical bug 报告了使用此插件编写的所有测试不起作用。直到我写这篇文章的那一天,插件开发人员都没有对此作出回应。作为这个开发者,Marc Palmer 有switched to iOS development and consultancy in August 2014 我不相信这个错误会得到解决。

      Webtest 插件 最后一次更新是在 3 年前,它“需要 Grails 1.2.RC2+”。我在运行它时遇到了缺少 webtest.jar 的丑陋错误,看起来这个插件还没有更新到当前的 Grails 版本。而且它的语法不是很像 Groovy,也不是很好。

      幸运的是Geb integration for Grails plugin 应该适用于当前的 Grails 版本。 :) 事实上,only 适用于 Grails 2.3.1+,截至撰写本文时,这些词最后一次更新是在 2014 年 6 月,它的 example tests project 已更新到 Grails 2.4.3,发布就在几天前,所以它是最新的。但是我还没有使用过这个项目。还看看 is 的示例代码,我不确定它是 RESTful API 测试的最佳选择 - 它更像是一个 GUI Web 应用程序测试工具..

      【讨论】:

        【解决方案3】:

        虽然可能不是很“grails 风格”,但我想应该可以使用 spring 上下文启动整个堆栈并使用 apache HttpClient 运行集成休息测试。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多