title: Embedded Elasticsearch junit webservice tests
body: <p>I'm working with an external elasticsearch instance and a spring boot app that exposes a bunch of rest services that will store data into an elasticsearch cluster. When deploying the spring boot app and configuring the repository settings to use the external elasticsearch instance then I can use the webservices and they will store into the repository and into elasticsearch fine. </p>
  
  <p>I'm now trying to modify my integration tests of these services to use an embedded elasticsearch instance to not rely on external resources. The cucumber tests use </p>
  
  <pre><code>@ContextConfiguration(classes = Application.class)
  @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  </code></pre>
  
  <p>Which starts up a jetty server instance to deploy the app on internally and then run a bunch of restassured calls to test the services. These tests were working fine having had used an internal hsqldb. However, switching to an embedded elasticsearch instance seems to not be working. I have some regular unit tests utilizing the embedded elasticsearch instance just directly using the service layer through code and that works fine...but when I use restassured to send an actual http request to the webservice endpoint the repositories don't seem to be working and it won't store into elasticsearch. My configuration is as follows: </p>
  
  <pre><code>Settings.Builder elasticsearchSettings = Settings.settingsBuilder()
                          .put("http.enabled", "true")
                          .put("cluster.name", "myCluster")
                          .put("path.home", "target/elasticsearch-home")
                          .put("elasticsearch.host","localhost")
                          .put("elasticsearch.port", "9300");
                          .put("path.data", dataDirectory.toString());
  
                  node = NodeBuilder.nodeBuilder()
                          .local(true)
                          .settings(elasticsearchSettings.build())
                          .node();
                  node.client().admin().indices().prepareCreate("dataset").get();
  </code></pre>
  
  <p>I can debug the tests and see externally through the browser that the elasticsearch instance and cluster are created through "<a href="http://localhost:9200/dataset/" rel="nofollow noreferrer">http://localhost:9200/dataset/</a>" but the webservice calls with restassured seem to not be able to store into the configured repository yet regular tests just calling repository.save seem to work fine. Does anyone know what additional step I'm missing that is required to have these webservices in a test environment be able to utilize the embedded elasticsearch instance when the webservice's dataservice utilizing the repositories is not autowired in the test itself but created through the spring boot start up process in the webservice classes? </p>
  
  <p>These webservice tests do work when using/configuring the external elasticsearch instance. It just somehow doesn't quite link the embedded one that does start fine and works with regular unit tests utilizing the same repositories with the jetty deployed services in an end to end test. </p>
  
  <p>Thanks much...please let me know if you need any additional information...it's a big process with a lot of moving parts so just trying to stick to the minimal set up. </p>