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?

3
  • Please do not upload images of code/data/errors. Instead, edit your question to include the code as properly formatted text. Commented Nov 16, 2023 at 5:46
  • 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. Commented Nov 16, 2023 at 7:49
  • 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. Commented Nov 16, 2023 at 10:29

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.