我正在尝试为一个类编写单元测试,该类使用 Google 的视觉 API 和 google-cloud-vision
库中的 AnnotatorImageClient
。
问题是我模拟的 AnnotatorImageClient
出于某种原因仍然调用真正的 batchAnnotateImages
方法然后抛出 NPE,这破坏了我的测试。
我以前从未在模拟中看到过这种行为,我想知道我是否做错了什么,spock/groovy 中是否存在错误,或者它是否与该 Google 库有关?
我已经检查过我的类中使用的对象是否真的是一个模拟对象,它确实是。我已经尝试使用 Spock 版本 1.2-groovy-2.5 和 1.3-groovy.2.5
被测试的类:
public class VisionClient {
private final ImageAnnotatorClient client;
@Autowired
public VisionClient(final ImageAnnotatorClient client) {
this.client = client;
}
public Optional<BatchAnnotateImagesResponse> getLabelsForImage(final Image image) {
var feature = Feature.newBuilder().setType(LABEL_DETECTION).build();
var request = AnnotateImageRequest.newBuilder()
.addFeatures(feature)
.setImage(image)
.build();
return Optional.ofNullable(client.batchAnnotateImages(singletonList(request)));
}
测试:
class VisionClientSpec extends Specification {
def "The client should use Google's client to call Vision API"() {
given:
def googleClientMock = Mock(ImageAnnotatorClient)
def visionClient = new VisionClient(googleClientMock)
def imageMock = Image.newBuilder().build()
when:
def resultOpt = visionClient.getLabelsForImage(imageMock)
then:
1 * googleClientMock.batchAnnotateImages(_ as List) >> null
!resultOpt.isPresent()
}
}
我希望模拟简单地返回 null
(我知道这个测试没有多大意义)。相反,它会调用引发 NPE 的 com.google.cloud.vision.v1.ImageAnnotatorClient.batchAnnotateImages
。
最佳答案
类 ImageAnnotatorClient
是用Java写的,方法batchAnnotateImages(List<AnnotateImageRequest> requests)
是final
.
Spock 能够模拟 Java final 类,但在模拟 Java final 方法方面不是很好。
关于java - Groovy Spock 模拟调用模拟类的真实方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55568798/