我正在使用Spock Framework
编写一个集成测试类,我的响应在groovyx.net.http.HttpResponseDecorator
我正在尝试使用以下步骤投射到我自己的类中,让我们说TestEntity
HttpResponseDecorator response = getRestClient().delete([path: "$BASE_URL"+"/96023"])
这里 Path 方法返回TestEntity
,现在我想获取那个实体并写断言
TestEntity entity = (TestEntity)response.getData()
当我写这句话时,我得到了ERROR!
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object
getData()
返回的类取决于返回您正在调用的 REST API 的内容类型。
添加此行以检查 Groovy 在您的情况下使用哪个类:
println response.data.getClass()
。或在 IDE 中使用调试来获取相同的信息。
例如,对于内容类型:
- 使用类
org.apache.groovy.json.internal.LazyMap
application/json
- 使用类
groovy.util.slurpersupport.NodeChild
application/xml
- 使用类
java.io.StringReader
text/plain
如果你正在测试后端,它使用TestEntity
类作为响应(例如在 Spring 中使用@RestController
和@RequestMapping
(,那么你在客户端中将没有TestEntity
实例(Spock 测试(。