# Mock RestHighLevelClient for elasticsearch

**URL:** <https://discuss.elastic.co/t/mock-resthighlevelclient-for-elasticsearch/283465>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [September 6, 2021, 3:46pm UTC](https://discuss.elastic.co/t/mock-resthighlevelclient-for-elasticsearch/283465 "2021-09-06T15:46:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![romeoopk](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@romeoopk](https://discuss.elastic.co/u/romeoopk)\
**Post date:** [September 6, 2021, 3:46pm UTC](https://discuss.elastic.co/t/mock-resthighlevelclient-for-elasticsearch/283465/1 "2021-09-06T15:46:53Z")

</div>

HI  
I would like to test my rest api in quarkus which internally calls elasticsearch using RestHighLevelClient.  
My problem is that the RestHighLevelClient is not mockable and hence I cannot execute my unit test to test the api.  
Any help is highly appreciated!  
Regards,

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 7, 2021, 3:35am UTC](https://discuss.elastic.co/t/mock-resthighlevelclient-for-elasticsearch/283465/2 "2021-09-07T03:35:06Z")

</div>

Welcome!

Some users shared their solutions in this issue.

> <https://github.com/elastic/elasticsearch/issues/40534>
>
> Hi,
> 
> I'm trying to unit test some code using RestHighLevelClient.
> 
> I have 
> …
> \`\`\`java
> private synchronized void createIndex(String indexName) throws IOException {
> if (!client.exists(new GetRequest(indexName), RequestOptions.DEFAULT)) {
> CreateIndexRequest request = new CreateIndexRequest(indexName);
> request.mapping(indexType, ElasticSearchConstants.MAPPING\_SOURCE);
> request.alias(new Alias(alias));
> request.settings(ElasticSearchConstants.MAPPING\_SETTINGS, XContentType.JSON);
> client.indices().create(request, RequestOptions.DEFAULT);
> }
> }
> \`\`\`
> 
> Now i want to verify that client.indices().create was called.
> 
> RestHighLevelClient is not final and I was able to mock it.
> 
> Mock for RestHighLevelClient.indices() returns null by default.
> So of course we want to mock that 
> 
> \`\`\`
> when(elasticSearchClient.indices()).thenReturn(mock(IndicesClient.class));
> \`\`\`
> 
> BAM problem. IndicesClient is final class. I can't mock it.
> 
> So I can't test if create gets called and with which parameters.
> 
> IndicesClient should not be final class or indices() should return an interface and your final class implementation is hidden away.
> 
> This is pretty much basic stuff for public APIs guys.

Would that work for you?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 5, 2021, 3:35am UTC](https://discuss.elastic.co/t/mock-resthighlevelclient-for-elasticsearch/283465/3 "2021-10-05T03:35:14Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
