mock restTemplate.exchange() 方法在 groovy spock 测试中抛出 MissingMethodExceptionin

问题描述 投票:0回答:1

实际的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()
方法失败。

unit-testing groovy mocking spock spring-boot-test
1个回答
0
投票

是在抱怨

args[2][0]
.

args[2]
是提供的
httpEntity
类型
org.springframework.http.HttpEntity
.

args[2][0]
是用于调用
args[2].getAt(0)
.

的 Groovy 语法糖

并且错误告诉您没有方法

HttpEntity#getAt
int
为参数,只有一个以
String
为参数。

© www.soinside.com 2019 - 2024. All rights reserved.