# Sharing connection junit and code under test

**URL:** <https://discuss.elastic.co/t/sharing-connection-junit-and-code-under-test/19999>\
**Category:** Elasticsearch\
**Created:** [September 29, 2014, 9:28am UTC](https://discuss.elastic.co/t/sharing-connection-junit-and-code-under-test/19999 "2014-09-29T09:28:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eunever32](https://avatars.discourse-cdn.com/v4/letter/e/e68b1a/32.png) [@eunever32](https://discuss.elastic.co/u/eunever32)\
**Post date:** [September 29, 2014, 9:28am UTC](https://discuss.elastic.co/t/sharing-connection-junit-and-code-under-test/19999/1 "2014-09-29T09:28:00Z")

</div>

I'm trying to connect to the "junit" embedded node in my code under test.

Normally the code under test makes a getNodeClient to 9300 to join the  
cluster.  
But I guess that's not an option with the embedded local(true) node.  
So what is the answer?

One option is to write the original code (under test) to accept a client as  
a parameter eg m(Client client, )  
And that would work but would require significant refactoring of existing  
code.

What do other people do?

Just to be clear, say I have a black box piece of code under test Class C  
with method m()

I start up my unit test and create a node and create a client

```
Node node = nodeBuilder().local(true).node()
node.start()
client = node.client()

```

Now I call m()

and somewhere else m calls es.writeRecord()  
which does a client = getClient() // get NodeClient  
but the two servers are separate and distinct.

Any way around this other than as I suggest : refactoring all code  
interfaces to pass in the client to be used?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/8cc197ba-68a9-4b93-9685-d652551b3731%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8cc197ba-68a9-4b93-9685-d652551b3731%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![eunever32](https://avatars.discourse-cdn.com/v4/letter/e/e68b1a/32.png) [@eunever32](https://discuss.elastic.co/u/eunever32)\
**Post date:** [October 1, 2014, 10:39pm UTC](https://discuss.elastic.co/t/sharing-connection-junit-and-code-under-test/19999/2 "2014-10-01T22:39:36Z")

</div>

Okay so here's the answer

Maybe my question wasn't clear.

I find the documentation isn't clear;;

Anyway to "create" a node in one thread :

Node node = nodeBuilder().local(true).node();  
Client client = node.client();

But then to connect to it in a separate thread from code that knows nothing  
about this client, just make sure the clustername is the same

and then :

Node node = nodeBuilder().\*local(true).\*client(true).node();  
Client client = node.client();

That fixes the problem.

On Monday, September 29, 2014 10:28:00 AM UTC+1, [eune...@gmail.com](mailto:eune...@gmail.com) wrote:

> I'm trying to connect to the "junit" embedded node in my code under test.
> 
> Normally the code under test makes a getNodeClient to 9300 to join the  
> cluster.  
> But I guess that's not an option with the embedded local(true) node.  
> So what is the answer?
> 
> One option is to write the original code (under test) to accept a client  
> as a parameter eg m(Client client, )  
> And that would work but would require significant refactoring of existing  
> code.
> 
> What do other people do?
> 
> Just to be clear, say I have a black box piece of code under test Class C  
> with method m()
> 
> I start up my unit test and create a node and create a client
> 
> ```
> Node node = nodeBuilder().local(true).node()
> node.start()
> client = node.client()
> 
> ```
> 
> Now I call m()
> 
> and somewhere else m calls es.writeRecord()  
> which does a client = getClient() // get NodeClient  
> but the two servers are separate and distinct.
> 
> Any way around this other than as I suggest : refactoring all code  
> interfaces to pass in the client to be used?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/d2ca075b-0caf-4e4d-a573-bc4526f596de%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d2ca075b-0caf-4e4d-a573-bc4526f596de%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 12:58am UTC](https://discuss.elastic.co/t/sharing-connection-junit-and-code-under-test/19999/3 "2017-07-06T00:58:50Z")

</div>


