Grails Spock检查私有对象的方法调用数

Grails Spock检查私有对象的方法调用数,grails,testing,spock,Grails,Testing,Spock,我已经为REST调用第三方api创建了一个服务。有一个RestBuilder实例: class ThirdPartyApiService{ ... private def restClient = new RestBuilder() ... } 在测试中,我想检查调用了多少次restClient.post(…)。试图用间谍来做这件事,但没有成功 class ThirdPartyApiServiceIntegrationSpec extends Integrati

我已经为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次调用)

我错过什么了吗?如何实现我的目标?

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


不,如何处理私人领域?如果使用您的建议,则无需在全局中使用:true param
setup: "prepare spies"
    def restBuilderSpy = GroovySpy(RestBuilder, global: true)
    thirdPartyApiService.restClient = restBuilderSpy