首页 > 解决方案 > 如何在 Grails / Spock 测试中打印响应而不是:org.grails.plugins.testing.GrailsMockHttpServletResponse@62c0fcae

问题描述

我正在为我的 Grails 后端编写 Spock 测试。我对在 Grails 中进行测试还很陌生,我正在尝试查看对我的模拟请求的响应。

当我编写 println(response) 时,我在标准输出中看到了这一点:org.grails.plugins.testing.GrailsMockHttpServletResponse@62c0fcae

而不是实际的响应。有没有办法查看这个模拟 http 响应的内容,而不是当前正在打印的内容?

标签: grailsprintlngroovy-console

解决方案


只需使用 Groovy 的转储方法:

生成显示其类、hashCode 和字段的对象的详细转储字符串。

println(response.dump())

我的例子:

def response = new GrailsMockHttpServletResponse()
println(response.dump())

输出:

<org.grails.plugins.testing.GrailsMockHttpServletResponse@6a79c292 outputStreamAccessAllowed=true writerAccessAllowed=true characterEncoding=ISO-8859-1 charset=false content= outputStream=org.springframework.mock.web.MockHttpServletResponse$ResponseServletOutputStream@21a947fe writer=null contentLength=0 contentType=null bufferSize=4096 committed=false locale=en_US cookies=[] headers=[:] status=200 errorMessage=null forwardedUrl=null includedUrls=[]>

推荐阅读