我正在使用域类作为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/