# Discovery Service for Unit Testing?

**URL:** <https://discuss.elastic.co/t/discovery-service-for-unit-testing/52599>\
**Category:** Elasticsearch\
**Created:** [June 13, 2016, 11:25am UTC](https://discuss.elastic.co/t/discovery-service-for-unit-testing/52599 "2016-06-13T11:25:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ndtreviv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndtreviv/32/22494_2.png) [@ndtreviv](https://discuss.elastic.co/u/ndtreviv)\
**Post date:** [June 13, 2016, 11:25am UTC](https://discuss.elastic.co/t/discovery-service-for-unit-testing/52599/1 "2016-06-13T11:25:19Z")

</div>

I've written some monitoring stuff that I'd really like to be able to unit test.

To do this well, it requires me to be able to run up a multi-node cluster.

As far as I understand it, I can run up multiple nodes with the same cluster name, but they won't actually join into one cluster without some sort of discovery service running. Is that correct?

Can anyone point me to some docs on how to run up a multi-node cluster for unit testing (ie: how to run a discovery service as part of testing)?

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![tanguy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tanguy/32/6030_2.png) [@tanguy](https://discuss.elastic.co/u/tanguy)\
**Post date:** [June 13, 2016, 1:02pm UTC](https://discuss.elastic.co/t/discovery-service-for-unit-testing/52599/2 "2016-06-13T13:02:30Z")

</div>

Hi,

Elasticsearch has a good testing framework that allows you to test your code against a testing cluster with random number of nodes. Have a look at `ESIntegTestCase` and classes that inherit from it. Note that this is used for integration tests, not unit tests.

---

<div class="post-metadata">

**Author:** ![ndtreviv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndtreviv/32/22494_2.png) [@ndtreviv](https://discuss.elastic.co/u/ndtreviv)\
**Post date:** [June 13, 2016, 1:29pm UTC](https://discuss.elastic.co/t/discovery-service-for-unit-testing/52599/3 "2016-06-13T13:29:01Z")

</div>

Thanks for that! I did see that, but they are integration tests, so they make the client available to you. Can you still connect to the "fake" cluster using a transport client?

My monitoring code uses a TransportClient to connect and perform health checks is all...

---

<div class="post-metadata">

**Author:** ![tanguy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tanguy/32/6030_2.png) [@tanguy](https://discuss.elastic.co/u/tanguy)\
**Post date:** [June 13, 2016, 1:43pm UTC](https://discuss.elastic.co/t/discovery-service-for-unit-testing/52599/4 "2016-06-13T13:43:13Z")

</div>

If your test class extend `ESIntegTestCase` you can get a `Client` instance using the `client()`method. The client will be either a TransportClient or a node Client, this is randomized. Most of the time your custom code should work with a Client instance.

---

<div class="post-metadata">

**Author:** ![ndtreviv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndtreviv/32/22494_2.png) [@ndtreviv](https://discuss.elastic.co/u/ndtreviv)\
**Post date:** [June 14, 2016, 9:58am UTC](https://discuss.elastic.co/t/discovery-service-for-unit-testing/52599/5 "2016-06-14T09:58:15Z")

</div>

Yeah, that's not going to work for me. I want my monitoring code to be able to instantiate its own TransportClient and connect to the cluster.

Is that possible?

---

<div class="post-metadata">

**Author:** ![ywelsch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ywelsch/32/7751_2.png) [@ywelsch](https://discuss.elastic.co/u/ywelsch)\
**Post date:** [June 15, 2016, 9:53am UTC](https://discuss.elastic.co/t/discovery-service-for-unit-testing/52599/6 "2016-06-15T09:53:04Z")

</div>

yes, in that case you need to know which port to connect to. Either you use the client() provided by `ESIntegTestCase` to find out what the port is, or you fix the port as part of the cluster setup, which can be done by overriding the method:

```auto
@Override
protected Settings nodeSettings(int nodeOrdinal) {
    // add setting specifying transport port
}

```

in your test class subclassing `ESIntegTestCase`

---

<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:43pm UTC](https://discuss.elastic.co/t/discovery-service-for-unit-testing/52599/7 "2017-07-05T22:43:43Z")

</div>


