# Best practices for automated tests

**URL:** <https://discuss.elastic.co/t/best-practices-for-automated-tests/320872>\
**Category:** Elasticsearch\
**Created:** [December 9, 2022, 10:13am UTC](https://discuss.elastic.co/t/best-practices-for-automated-tests/320872 "2022-12-09T10:13:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PzYon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pzyon/32/114456_2.png) [@PzYon](https://discuss.elastic.co/u/PzYon)\
**Post date:** [December 9, 2022, 10:13am UTC](https://discuss.elastic.co/t/best-practices-for-automated-tests/320872/1 "2022-12-09T10:13:51Z")

</div>

In our (Java) unit tests we would like to test the integration of Elasticsearch (e.g. do queries work, can docs be indexed correctly, etc.). I'm wondering if there are any best practices or recommendations regarding this topic?

Here are some additional thoughts/questions from my side:

- My preferred solution would be to run the test against a real, "always-running" Elasticsearch instance. However in our current company/environment this is not possible.
- For certain ORMs (e.g. Entity Framework in the dotnet world) there exist in-memory representations that are meant to be used for tests. Is there something similar in the elastic world?
- I found the `ESSingleNodeTestCase`-class. However I didn't find much documentation nor any examples, etc. Plus I read that it can be quite tedious to set up. Is This still supported and recommended?
- The requirement is that these tests can be executed locally (e.g. in IntelliJ) as well as on our build server.

Thanks in advance for any answers. 🙂

---

<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:** [December 9, 2022, 10:57am UTC](https://discuss.elastic.co/t/best-practices-for-automated-tests/320872/2 "2022-12-09T10:57:56Z")

</div>

To do integration tests, you can:

- ~~Use the Gradle tools elasticsearch already has. You can read some information about this here: [#21119](https://github.com/elastic/elasticsearch/issues/21119). This [sample project](https://github.com/joel-costigliola/elastic-search-test) shows how to use Elasticsearch Test Classes~~
- Use the Maven plugin: [GitHub - alexcojocaru/elasticsearch-maven-plugin: A Maven plugin to run a single node Elasticsearch cluster during the integration test phase of a build](https://github.com/alexcojocaru/elasticsearch-maven-plugin)
- Use Ant scripts like [https://david.pilato.fr/blog/2016-07-29-elasticsearch-real-integration-tests/](http://david.pilato.fr/blog/2016/10/18/elasticsearch-real-integration-tests-updated-for-ga/)
- Using Docker and TestContainers: [Elasticsearch container - Testcontainers for Java](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.

---

<div class="post-metadata">

**Author:** ![PzYon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pzyon/32/114456_2.png) [@PzYon](https://discuss.elastic.co/u/PzYon)\
**Post date:** [December 9, 2022, 2:08pm UTC](https://discuss.elastic.co/t/best-practices-for-automated-tests/320872/3 "2022-12-09T14:08:02Z")

</div>

Thanks a lot for your quick reply. I agree that unit tests should not access or require any third party tool. On the other hand I think it's helpful to unit test in a "as real as possible"-situation. That's why some kind of in-memory or something like that would be cool.

Would that maybe be possible with the `ESSingleNodeTestCase`?

As far as I can see, all the mentioned solutions require Docker or download some other stuff.

Thank you very much. 🙂

---

<div class="post-metadata">

**Author:** ![PzYon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pzyon/32/114456_2.png) [@PzYon](https://discuss.elastic.co/u/PzYon)\
**Post date:** [December 30, 2022, 2:47pm UTC](https://discuss.elastic.co/t/best-practices-for-automated-tests/320872/4 "2022-12-30T14:47:34Z")

</div>

FYI - I just posted a follow up question here:

> [@ESSingleNodeTestCase / unit tests / official recommendations?](https://discuss.elastic.co/t/essinglenodetestcase-unit-tests-official-recommendations/322222):
>
> I asked this question regarding unit tests a couple of weeks ago: [Best practices for automated tests](https://discuss.elastic.co/t/best-practices-for-automated-tests/320872) Now I would like to come back to the ESSingleNodeTestCase-class. I managed to get it to start an elastic server with the steps mentioned at the end of this post in case anybody is interested. Now I realized that the Client provided by the ESSingleNodeTestCase is from the org.elasticsearch.client.internal-package. However the regular client that we are using with API version 8 is coming from th…

---

<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:** [January 27, 2023, 2:47pm UTC](https://discuss.elastic.co/t/best-practices-for-automated-tests/320872/5 "2023-01-27T14:47:42Z")

</div>

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