如何为 OpenSearchClient API 编写 java 单元测试

问题描述 投票:0回答:1

我正在尝试在 Spring Java 中为 org.opensearch.client.opensearch.OpenSearchClient API(如 msearch、bulk 等)编写 Junit 测试,但出现编译错误。

被嘲笑的API签名是

public <TDocument> MsearchResponse<TDocument> msearch(MsearchRequest request, Class<TDocument> tDocumentClass)
            throws IOException, OpenSearchException

单元测试模拟方法的示例是;

Mockito.when(client.msearch(ArgumentMatchers.any(), ArgumentMatchers.any()))
            .thenReturn(mSearchResponse);

看到的错误消息如下'

方法 msearch(MsearchRequest, Class) 对于 输入 OpenSearchClient

任何人都可以指导、分享如何正确模拟和测试 OpenSearchClient API?

java unit-testing mocking mockito opensearch
1个回答
0
投票

我通过将 Mockito when-then 语句更改为下面找到了解决方案;

Mockito.when(client.msearch(ArgumentMatchers.<MsearchRequest>any(), ArgumentMatchers.<Class<Map>>any())).thenReturn(mSearchResponse);

上述问题已为我解决。 干杯,快乐编码!

© www.soinside.com 2019 - 2024. All rights reserved.