P3arl
P3arl

Reputation: 391

How can I do integration testing in elasticsearch with spring-data-elasticsearch?

I am using spring-data-elasticsearch v3.2.4.RELEASE which is available via spring-boot-starter-data-elasticsearch v2.2.4.RELEASE. I want to do the integration tests for this but the available option which is this: https://github.com/allegro/embedded-elasticsearch not working.

The part which I tried to get started as POC is below and it is throwing exception:

    public class EmbeddedElasticConfiguration {

    public static final String VERSION = "6.8.4";
    public static final String DOWNLOAD_DIRECTORY = "<path>\\test-elasticsearch";
    public static final String INSTALLATION_DIRECTORY = "<path>\test-elasticsearch";
    public static final String NAME = "elasticsearch";
    public static final String TRANSPORT_PORT = "9300";
    public static final String HTTP_CLIENT_PORT = "9200";
    public static final String TEST_INDEX = "salesorder";
    public static final String TEST_TYPE = "salesorder";
    public static final String RESOURCE_LOCATION = "src/test/resources/salesorder-mapping.json";
    private ObjectMapper objectMapper = new ObjectMapper();
    EmbeddedElastic embeddedElastic;

    @Test
    public void configure() throws IOException, InterruptedException {
        embeddedElastic = EmbeddedElastic.builder()
                .withElasticVersion(VERSION)
                .withSetting(TRANSPORT_TCP_PORT, 9300)
                .withSetting(CLUSTER_NAME, "my-cluster")
                //.withPlugin("analysis-stempel")
                .withDownloadDirectory(new File(DOWNLOAD_DIRECTORY))
                .withInstallationDirectory(new File(INSTALLATION_DIRECTORY))
                .withSetting(HTTP_PORT, 9200)
                .withIndex(TEST_INDEX, IndexSettings.builder()
                       .withType(TEST_TYPE, readMappingFromJson())
                     .build())
                .build();

        embeddedElastic.start();
    }

    private String readMappingFromJson() throws IOException {
        final File file = ResourceUtils.getFile(RESOURCE_LOCATION);
        String mapping = new String(Files.readAllBytes(file.toPath()));
        System.out.println("mapping: "+ mapping);
        return mapping;
    }

    @After
    public void stopServer() {
        embeddedElastic.stop();
    }
}

I am below getting below exception:

pl.allegro.tech.embeddedelasticsearch.EmbeddedElasticsearchStartupException: Failed to start elasticsearch within time-out

    at pl.allegro.tech.embeddedelasticsearch.ElasticServer.waitForElasticToStart(ElasticServer.java:127)
    at pl.allegro.tech.embeddedelasticsearch.ElasticServer.start(ElasticServer.java:50)
    at pl.allegro.tech.embeddedelasticsearch.EmbeddedElastic.startElastic(EmbeddedElastic.java:82)
    at pl.allegro.tech.embeddedelasticsearch.EmbeddedElastic.start(EmbeddedElastic.java:63)
    at com.xxx.elasticsearch.adapter.configuration.EmbeddedElasticConfiguration.configure(EmbeddedElasticConfiguration.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

Can someone help with any other options for integration tests in elasticsearch with spring-data or How should I write integration tests for elasticsearch.

I know there are other answers on stackoverflow and other portals for embedded-elasticsearch but those are not working with my current elasticsearch version.

Upvotes: 3

Views: 5727

Answers (1)

Abacus
Abacus

Reputation: 19451

You did not write what version of JUnit you are using. I can tell you how we handle this in Spring Data Elasticsearch itself:

For JUnit 4 you can check the JUnit 4 Rule that uses the Utils class to set up a local running Elasticsearch node and tears it down at the end.

For JUnit 5 you might have a look at how this is handled in the current master branch,the relevant classes are found here.

By using the annotation SpringIntegrationTest a local Elasticsearch is started and automatically shut down when all tests are done. Internally there is quite some work done in setting the cluster up, getting the info into the JUnit extension and enabling Spring autowiring of the relevant information into the configuration class. This setup is quite complex, but in the end it uses the same Utils class mentioned above.

I hope this gives a good starting point

Upvotes: 3

Related Questions