# Resthighlevel client unit test is not working

**URL:** <https://discuss.elastic.co/t/resthighlevel-client-unit-test-is-not-working/311897>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [August 11, 2022, 6:20am UTC](https://discuss.elastic.co/t/resthighlevel-client-unit-test-is-not-working/311897 "2022-08-11T06:20:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Apurba](https://avatars.discourse-cdn.com/v4/letter/a/edb3f5/32.png) [@Apurba](https://discuss.elastic.co/u/Apurba)\
**Post date:** [August 11, 2022, 6:20am UTC](https://discuss.elastic.co/t/resthighlevel-client-unit-test-is-not-working/311897/1 "2022-08-11T06:20:25Z")

</div>

I have written a method, below

```auto
    @Inject
    private RestHighLevelClient esClient;

    public SearchResponse getData(SearchRequest searchRequest) {
        try {
            SearchResponse searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT);
            log.info("Successfully fetched the data from ES");
            return searchResponse;
        } catch (ElasticsearchException e) {
            log.error("ES exception occurs on fetching data.");
        } catch (IOException e) {
            log.error("Runtime exception occurs on fetching data from ES.");
        }
    }

```

I have tried several way to write the unit test of it but sometime it gives null pointer exception, sometime can not mock a final class, final method etc. I have tried almost every way. So can anyone help me here by saying how I should write the unit test for this method.

---

<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:** [August 11, 2022, 1:08pm UTC](https://discuss.elastic.co/t/resthighlevel-client-unit-test-is-not-working/311897/2 "2022-08-11T13:08:20Z")

</div>

You should use the [Elasticsearch Java API Client [8.3] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/index.html).

I think you can mock the objects easily...

Otherwise have a look at:

> <https://github.com/elastic/elasticsearch/issues/40534#issuecomment-501195400>
>
> 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.

---

<div class="post-metadata">

**Author:** ![Apurba](https://avatars.discourse-cdn.com/v4/letter/a/edb3f5/32.png) [@Apurba](https://discuss.elastic.co/u/Apurba)\
**Post date:** [August 18, 2022, 6:05am UTC](https://discuss.elastic.co/t/resthighlevel-client-unit-test-is-not-working/311897/3 "2022-08-18T06:05:23Z")

</div>

It worked, thanks @dadoonet

---

<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:** [September 15, 2022, 6:05am UTC](https://discuss.elastic.co/t/resthighlevel-client-unit-test-is-not-working/311897/4 "2022-09-15T06:05:34Z")

</div>

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