How to write java unit test for OpenSearchClient APIs
Asked Answered
R

1

0

I am trying to write a Junit test in Spring Java for org.opensearch.client.opensearch.OpenSearchClient APIs like msearch, bulk, etc. but I get compilation error.

The API signature being mocked is

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

The example of unit test mock approach is;

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

Error message seen was as below'

The method msearch(MsearchRequest, Class) is ambiguous for the type OpenSearchClient

Can anyone guide, share how can correctly mock and test OpenSearchClient APIs?

Rosierosily answered 16/11, 2023 at 5:40 Comment(3)
Please do not upload images of code/data/errors. Instead, edit your question to include the code as properly formatted text.Trow
I had ensured nothing extra other than required generic code and error information was part of image but thanks for the link information. I have edited my question for this. Thanks @seenukarthi.Rosierosily
Thanks for editing your post. The problem with code/errors as images is it cannot be copied, which helps the community to search or test the code or the errors. If you feel the image helps to state your problem better please add it in addition to the text.Trow
R
1

I found the solution by changing my Mockito when-then statement to below;

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

The Map in above is what I expect my Response enclosing type to be in. If different type for anyone then they can replace the same with their actual expected type. The said issue is resolved for me.

Cheers and happy coding!

Rosierosily answered 16/11, 2023 at 7:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.