# TestContainers elasticsearch testing module 0.1-SNAPSHOT

**URL:** https://discuss.elastic.co/t/testcontainers-elasticsearch-testing-module-0-1-snapshot/117373
**Category:** Community Ecosystem
**Created:** [January 28, 2018, 3:53pm UTC](https://discuss.elastic.co/t/testcontainers-elasticsearch-testing-module-0-1-snapshot/117373 "2018-01-28T15:53:51Z")
**Posts on this page:** 2
**Page:** 1

<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: [January 28, 2018, 3:53pm UTC](https://discuss.elastic.co/t/testcontainers-elasticsearch-testing-module-0-1-snapshot/117373/1 "2018-01-28T15:53:51Z")

</div>

Heya

I'm pleased to announce that I just published a preview version of the "TestContainers elasticsearch testing module"

It's a [Testcontainers](https://www.testcontainers.org/) module for [elasticsearch](https://www.elastic.co/products/elasticsearch).

Note that it's based on the [official Docker image](https://www.elastic.co/guide/en/elasticsearch/reference/6.1/docker.html) provided by elastic.

Basically it helps to start an elasticsearch instance from the JVM for integration tests purpose for example. It is not meant to be run in production.

See [testcontainers.org](https://www.testcontainers.org) for more information about Testcontainers.

I'd love to hear your feedbacks about this before I release a first version on Maven central!  
You can either comment here, in a new thread on #elasticsearch or open an issue in:

> **[dadoonet/testcontainers-java-module-elasticsearch](https://github.com/dadoonet/testcontainers-java-module-elasticsearch)**
>
> testcontainers-java-module-elasticsearch - Dockerized Elasticsearch container for testing under Testcontainers

## Usage example

You can start an elasticsearch container instance from any Java application by using:

```java
// Specify the version you need.
ElasticsearchContainer container = new ElasticsearchContainer()
        .withVersion("6.1.2");

// Optional: you can also set what is the Docker registry you want to use with.
container.withBaseUrl("docker.elastic.co/elasticsearch/elasticsearch");

// Optional: define which plugin you would like to install.
// It will download it from internet when building the image
container.withPlugin("discovery-gce");

// Optional: define the plugins directory which should contain plugins ZIP files you want to install.
// Note that if you want to just download an official plugin, use withPlugin(String) instead.
container.withPluginDir(Paths.get("/path/to/zipped-plugins-dir"));

// Optional: you can also change the password for X-Pack (for versions >= 6.1).
container.withEnv("ELASTIC_PASSWORD", "changeme");

// Configure the container (mandatory).
container.configure();

// Start the container.
container.start();

// Do whatever you want here.
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "changeme"));
RestClient client = RestClient.builder(container.getHost())
        .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
        .build();
Response response = client.performRequest("GET", "/");

// Stop the container.
container.stop();

```

## JUnit 4 Usage example

Running elasticsearch as a resource during a test:

```java
public class SomeTest {
    @Rule
    public ElasticsearchResource elasticsearch = new ElasticsearchResource();

    @Test
    public void someTestMethod() {
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("elastic", "changeme"));

        RestClient client = RestClient.builder(elasticsearch.getHost())
                .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
                .build();
        Response response = client.performRequest("GET", "/");
        assertThat(response.getStatusLine().getStatusCode(), is(200));

```

The default version is read from a property file named `elasticsearch.properties` in `fr.pilato.elasticsearch.containers` package.  
If you provide an `elasticsearch.properties` in `fr.pilato.elasticsearch.containers` package,  
the settings will be read from it:

```auto
baseUrl=docker.elastic.co/elasticsearch/elasticsearch
version=6.1.2

```

You can also define this programmatically with:

```auto
@Rule
public ElasticsearchResource elasticsearch = new ElasticsearchResource(
    "docker.elastic.co/elasticsearch/elasticsearch", // baseUrl (can be null)
    "6.1.2", // version
    Paths.get("/path/to/zipped-plugins-dir"), // pluginsDir (can be null)
    "changeme"); // X-Pack security password to set (can be null)

```

## Dependency information

### Maven

```auto
<dependency>
    <groupId>fr.pilato.elasticsearch.testcontainers</groupId>
    <artifactId>testcontainers-elasticsearch</artifactId>
    <version>0.1-SNAPSHOT</version>
</dependency>

```

### Gradle

```auto
compile group: 'fr.pilato.elasticsearch.testcontainers', name: 'testcontainers-elasticsearch', version: '0.1-SNAPSHOT'

```

## License

Apache2 License.

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [June 23, 2020, 6:03am UTC](https://discuss.elastic.co/t/testcontainers-elasticsearch-testing-module-0-1-snapshot/117373/2 "2020-06-23T06:03:53Z")

</div>


