Class ElasticSearchDockerResource

java.lang.Object
org.junit.rules.ExternalResource
no.mnemonic.commons.junit.docker.DockerResource
no.mnemonic.commons.junit.docker.ElasticSearchDockerResource
All Implemented Interfaces:
org.junit.rules.TestRule

public class ElasticSearchDockerResource extends DockerResource
ElasticSearchDockerResource is a JUnit resource which can be used to write integration tests against an ElasticSearch server executed inside an isolated Docker container. It extends the basic DockerResource and makes sure that the container initialization waits until ElasticSearch is available. Data indexed into ElasticSearch by tests can be truncated by providing the indices to delete when constructing the resource and calling deleteIndices().

Initialize ElasticSearchDockerResource in the following way as a ClassRule:

 @ClassRule
  public static ElasticSearchDockerResource elastic = ElasticSearchDockerResource.builder()
     .setImageName("elasticsearch")
     .addApplicationPort(9200)
     .addApplicationPort(9300)
     .addDeleteIndex("foo")
     .addDeleteIndex("bar")
     .addDeleteIndex("baz")
     .build();
 
See DockerResource.Builder and ElasticSearchDockerResource.Builder for more information on the configuration properties.
  • Method Details

    • deleteIndices

      public void deleteIndices()
      Truncate data indexed into ElasticSearch by deleting the indices specified when constructing the resource. If no indices were specified all indices will be deleted.
      Throws:
      IllegalStateException - If indices could not be deleted
    • builder

      public static ElasticSearchDockerResource.Builder builder()
      Create builder for ElasticSearchDockerResource.
      Returns:
      Builder object
    • isContainerReachable

      protected boolean isContainerReachable()
      Verifies that ElasticSearch is reachable by querying the cluster status from inside the Docker container.
      Overrides:
      isContainerReachable in class DockerResource
      Returns:
      True if cluster status could be queried successfully
      Throws:
      IllegalStateException - If cluster status could not be queried