How to control the response with the Elasticsearch async api How to control the response with the Elasticsearch async api elasticsearch elasticsearch

How to control the response with the Elasticsearch async api


Mock RestHighLevelClient then inside indexAsync mock IndexResponse and pass it to the ActionListener.

RestHighLevelClient restHighLevelClient = mock(RestHighLevelClient.class);when(restHighLevelClient.indexAsync(any(), any(), any())).then(a -> {    ActionListener<IndexResponse> listener = a.getArgument(2);    IndexResponse response = mock(IndexResponse.class);    when(response.getResult()).then(b -> {        return Result.UPDATED;    });    listener.onResponse(response);    return null;});MyHelper myHelper = new MyHelper(restHighLevelClient);Boolean result = myHelper.doIt(null).get();assertFalse(result);

Also, configure Mockito to support mocking final methods otherwise a NPE will be thrown when mocking indexAsync.

Option 1

Instead of using the mockito-core artifact, include the mockito-inline artifact in your project

Option 2

Create a file src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker with mock-maker-inline as the content