实际的java代码
metadataContentList.addAll((restTemplate.exchange(SERVICE_URL + queryForIds, HttpMethod.GET, httpEntity, DocMetadataResponse.class, map).getBody()).getResults());
这是一个模拟休息电话
_ * mockRestTemplate.exchange(_ as String, _, _, DocMetadataResponse.class, _) >> { args ->
def ids = (args[0] as String).split("\\?")[1].split("&").collect {
it.split("=")[1]
}
new DocMetadataResponse(ids.size(), metadataContentList.findAll { args[2][0] == it.contextId })
}
它抛出异常为
groovy.lang.MissingMethodException: No signature of method: org.springframework.http.HttpEntity.getAt() is applicable for argument types: (Integer)
values: [0]
Possible solutions: getAt(java.lang.String), putAt(java.lang.String, java.lang.Object), wait(), getBody(), grep(), tap(groovy.lang.Closure)
at com.mrll.javelin.drmaudit.service.DrmAuditServiceIntSpec.$spock_feature_0_1_closure12$_closure22
这很好用,但是根据新要求,我不得不使用
restTemplate.exchange()
方法,因为我必须传递一些标头参数
_ * mockRestTemplate.getForObject(_ as String, DocMetadataResponse.class, _)
我是 Groovy 的新手,所以无法理解为什么它会抛出
exchange
方法的错误?
我尝试从中存根 mock
ResponseEntity
和 getBody
但后来我得到 NullPointerException
所有其他模拟 restcall 正在使用
getForObject
和 postForObject
但使用 exchange()
方法失败。
是在抱怨
args[2][0]
.
args[2]
是提供的 httpEntity
类型 org.springframework.http.HttpEntity
.
args[2][0]
是用于调用 args[2].getAt(0)
. 的 Groovy 语法糖
并且错误告诉您没有方法
HttpEntity#getAt
以int
为参数,只有一个以String
为参数。