# Elasticsearch high level java client mock test

**URL:** <https://discuss.elastic.co/t/elasticsearch-high-level-java-client-mock-test/287571>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [October 25, 2021, 11:38am UTC](https://discuss.elastic.co/t/elasticsearch-high-level-java-client-mock-test/287571 "2021-10-25T11:38:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![111505](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/111505/32/94191_2.png) [@111505](https://discuss.elastic.co/u/111505)\
**Post date:** [October 25, 2021, 11:38am UTC](https://discuss.elastic.co/t/elasticsearch-high-level-java-client-mock-test/287571/1 "2021-10-25T11:38:45Z")

</div>

I am using a RestHighlevelclient in the Spring Boot environment.  
I want to create a JUnit Test Code, but I can not find a way.  
Is there no test method to officially provide?  
I want to test the normal operation of basic queries. Is there no way?

---

<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:** [October 25, 2021, 3:39pm UTC](https://discuss.elastic.co/t/elasticsearch-high-level-java-client-mock-test/287571/2 "2021-10-25T15:39:41Z")

</div>

To run integration tests (not unit tests) you should run an Elasticsearch node/cluster in something close to a production environment, like a real Elasticsearch server instance.

To do integration tests, you can for example:

- Using Docker and TestContainers: [Elasticsearch container - Testcontainers](https://www.testcontainers.org/modules/elasticsearch/)
- Using Docker from maven: [fscrawler/pom.xml at e15dddf72b1ed094dad279d492e4e0314f73683f · dadoonet/fscrawler · GitHub](https://github.com/dadoonet/fscrawler/blob/e15dddf72b1ed094dad279d492e4e0314f73683f/pom.xml#L241-L289)

For sure, integration tests must be run against a real Elasticsearch instance.  
Unit tests should not require any third party tool to run IMHO.

For Unit Test, you could read what users have been sharing 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.

Hope this helps.

---

<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:** [November 22, 2021, 3:40pm UTC](https://discuss.elastic.co/t/elasticsearch-high-level-java-client-mock-test/287571/3 "2021-11-22T15:40:07Z")

</div>

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