rest - 如何在Grails中将领域类作为REST资源编写集成测试?

标签 rest grails resources integration-testing grails-domain-class

我正在使用域类作为Grails documentation中提到的休息资源

因此,在使用这种方法时,不会创建任何 Controller 或服务类。而且,我试图找到一种方法来编写用于REST的域集成测试,但找不到。因此,请告诉我该怎么做或将链接发布到一个可以说明同样情况的地方。

关于这种方法,这正是要求的确切方法,因此我无法更改为使用REST服务的其他方式。

提前致谢。

最佳答案

您应该考虑使用Spock作为测试框架:
https://spock-framework.readthedocs.org/en/latest/

示例如何将其用于REST:

class ExampleWebAppSpecification extends Specification {
    def "Should return 200 & a message with the input appended"() {

        setup:
             def primerEndpoint = new RESTClient( 'http://localhost:8080/' )
        when:
             def resp = primerEndpoint.get([ path: 'exampleendpoint', query : [ input : 'Get a hair cut' ]])
        then:
             with(resp) {
                 status == 200
                 contentType == "application/json"
             }
             with(resp.data) {
                 payload == "Something really important: Get a hair cut"
             }
    }
}

编辑-
在buildConfig.groovy中:
compile("org.codehaus.groovy:groovy-all:2.2.0")
compile("com.fasterxml.jackson.core:jackson-databind")

testCompile("org.spockframework:spock-core:0.7-groovy-2.0")
testCompile("org.codehaus.groovy.modules.http-builder:http-builder:0.7+")
testCompile("net.sf.json-lib:json-lib:2.4+")

关于rest - 如何在Grails中将领域类作为REST资源编写集成测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28020792/

相关文章:

c# - 作为可重用 XAML 片段的矢量图像

java - Amazon Java SDK 与 Jersey 的 REST - 无法验证

java - 带有请求消息的 Apache CXF ExceptionMapper

javascript - 保护公共(public) REST API 的安全

.net - 如何比较多个 .resx 文件?

android - 如何获取数组的名称

Spring MVC 映射-未找到 404 错误-使用 Gradle 和 Jetty

unit-testing - 了解Grails的mockFor需求

json - Grails 3如何将json View 呈现到文件输出而不是http响应流?

grails - 如何获得<g:select…>的内容