【发布时间】:2021-06-27 12:12:52
【问题描述】:
我正在编写 Spock 测试,并使用内联闭包来存根以实现简单的失败/通过行为。
def "test timeout"() {
given:
2 * feignClient.poll("foo") >>
{
int retries = 0;
if (retries < 1) {
retries++
throw newRetryable()
}
pollWaitSuccessResponseEntity
}
所以我尝试将闭包重构为一个命名的闭包:
def retryClosure = {
int retries = 0;
if (retries < 1) {
retries++
throw newRetryable()
}
pollWaitSuccessResponseEntity
}
2 * feignClient.poll("foo") >> retryClosure
测试失败并出现以下错误:
Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object
'com.example.service.FooServiceTest$__spock_initializeFields_closure2@3243178c' with class 'com.example..FooServiceTest$__spock_initializeFields_closure2' to class 'org.springframework.http.ResponseEntity'
【问题讨论】: