# Elasticsearch for JUnits

**URL:** <https://discuss.elastic.co/t/elasticsearch-for-junits/52409>\
**Category:** Elasticsearch\
**Created:** [June 10, 2016, 6:30am UTC](https://discuss.elastic.co/t/elasticsearch-for-junits/52409 "2016-06-10T06:30:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![A.Aravindhan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/a.aravindhan/32/4872_2.png) [@A.Aravindhan](https://discuss.elastic.co/u/A.Aravindhan)\
**Post date:** [June 10, 2016, 6:30am UTC](https://discuss.elastic.co/t/elasticsearch-for-junits/52409/1 "2016-06-10T06:30:08Z")

</div>

We are using Elasticsearch and planning to have JUnits for our modules. These JUnits run during our product build infact as a first step... we are looking for a kind of embedded Elasticsearch which we can use for our JUnits as we don't want to have a running instance in a VM and use it... (or) are there any recommendations to mock Elasticsearch APIs?

---

<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:** [June 10, 2016, 6:44am UTC](https://discuss.elastic.co/t/elasticsearch-for-junits/52409/2 "2016-06-10T06:44:04Z")

</div>

One of the easiest thing to do ATM is to extend `ESIntegTestCase` and add this dependency in your project:

```auto
        <dependency>
            <groupId>org.elasticsearch.test</groupId>
            <artifactId>framework</artifactId>
            <version>5.0.0-alpha3</version>
            <scope>test</scope>
        </dependency>

```

And something like this:

```auto
            <plugin>
                <!-- we skip surefire to work with randomized testing above -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.carrotsearch.randomizedtesting</groupId>
                <artifactId>junit4-maven-plugin</artifactId>
                <version>2.3.3</version>

                <!-- Defaults for all tests (ITs and Unit). -->
                <configuration>
                    <heartbeat>10</heartbeat>
                    <jvmOutputAction>pipe,ignore</jvmOutputAction>
                    <leaveTemporary>true</leaveTemporary>
                    <ifNoTests>warn</ifNoTests>
                    <parallelism>${tests.jvms}</parallelism>
                    <assertions enableSystemAssertions="false">
                        <enable/>
                        <disable package="${tests.assertion.disabled}"/>
                        <!-- pass org.elasticsearch to run without assertions -->
                    </assertions>

                    <listeners>
                        <report-text showThrowable="true" showStackTraces="true" showOutput="${tests.output}" showStatusOk="true"
                                     showStatusError="true" showStatusFailure="true" showStatusIgnored="true" showSuiteSummary="true" />
                    </listeners>
                    <seed>${tests.seed}</seed>
                    <systemProperties combine.children="append">
                        <arg.common>arg.common</arg.common>
                        <tests.locale>${tests.locale}</tests.locale>
                        <tests.nightly>${tests.nightly}</tests.nightly>
                    </systemProperties>
                </configuration>

                <executions>
                    <execution>
                        <id>unit-tests</id>
                        <phase>test</phase>
                        <goals>
                            <goal>junit4</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

```

Hope this helps

---

<div class="post-metadata">

**Author:** ![A.Aravindhan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/a.aravindhan/32/4872_2.png) [@A.Aravindhan](https://discuss.elastic.co/u/A.Aravindhan)\
**Post date:** [June 10, 2016, 7:07am UTC](https://discuss.elastic.co/t/elasticsearch-for-junits/52409/3 "2016-06-10T07:07:31Z")

</div>

Thanks for the reply. I haven't understood the usage of what you have said.

Where do the Elasticsearch run?  
How do I use it in my JUnit class?

We are looking for something like the one below ,

Endpoint.publish("[http://localhost:9292/ws/milli](http://localhost:9292/ws/milli)", new HelloWorld()); which opens up a http port on 9292..

similar thing for Elasticsearch... !!!  
@Before  
start the Elasticsearch  
Run the junits  
@Shutdown  
shutdown the Elasticsearch

---

<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:** [June 10, 2016, 7:36am UTC](https://discuss.elastic.co/t/elasticsearch-for-junits/52409/4 "2016-06-10T07:36:54Z")

</div>

The test framework will start an embedded cluster for you if you extend the class I mentioned.

---

<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:** [July 5, 2017, 10:44pm UTC](https://discuss.elastic.co/t/elasticsearch-for-junits/52409/5 "2017-07-05T22:44:50Z")

</div>


