我已经为REST call 第3方api创建了服务。有一个RestBuilder实例:

class ThirdPartyApiService{
    ...
    private def restClient = new RestBuilder()
    ...
}

在测试中,我想检查 restClient.post(...)被调用了多少次。试图使用 spy ,但没有成功。
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次调用)

我错过了什么吗?我怎样才能达到目标?

最佳答案

您只是在测试中缺少了一行漂亮的代码。 ;)

setup: "prepare spies"
    def restBuilderSpy = GroovySpy(RestBuilder, global: true)
    thirdPartyApiService.restClient = restBuilderSpy

08-06 20:29