我为REST调用第三方api创建了一项服务。有一个RestBuilder实例:
class ThirdPartyApiService{
...
private def restClient = new RestBuilder()
...
}
在测试中,我想检查调用 restClient.post(...)的次数。试图使用间谍但没有成功。
class ThirdPartyApiServiceIntegrationSpec extends IntegrationSpec {
def thirdPartyApiService
def "test smthing"() {
setup: "prepare spies"
def restBuilderSpy = GroovySpy(RestBuilder, global: true)
when:
thirdPartyApiService.someMethod()
then:
1 * restBuilderSpy.post(* _)
}
}
它失败了
的 |调用次数太少:
1 * restBuilderSpy.post(* _)(0次调用)
我错过了什么吗?我怎样才能实现我的目标?
答案 0 :(得分:0)
您在测试中缺少一行漂亮的代码。 ;)
setup: "prepare spies"
def restBuilderSpy = GroovySpy(RestBuilder, global: true)
thirdPartyApiService.restClient = restBuilderSpy