如何使用 spring-data-elasticsearch 在 elasticsearch 中进行集成测试? [英] How can I do integration testing in elasticsearch with spring-data-elasticsearch?

查看:43
本文介绍了如何使用 spring-data-elasticsearch 在 elasticsearch 中进行集成测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 spring-data-elasticsearch v3.2.4.RELEASE,它可以通过 spring-boot-starter-data-elasticsearch v2.2.4.RELEASE 获得.我想为此进行集成测试,但可用的选项是:https://github.com/allegro/embedded-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.

我尝试作为 POC 开始的部分在下面,它抛出异常:

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();
    }
}

我低于异常:

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)

有人可以帮助使用 spring-data 在 elasticsearch 中进行集成测试的任何其他选项,或者我应该如何为 elasticsearch 编写集成测试.

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

我知道在 stackoverflow 和其他嵌入式弹性搜索门户网站上还有其他答案,但这些答案不适用于我当前的弹性搜索版本.

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

推荐答案

您没有编写所使用的 JUnit 版本.我可以告诉你我们如何在 Spring Data Elasticsearch 中处理这个问题:

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

对于 JUnit 4,您可以查看 JUnit 4 Rule 使用 Utils class 设置本地运行的 Elasticsearch 节点并在最后将其拆除.

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.

对于 JUnit 5,您可能会查看当前 master 分支中的处理方式,相关类 在这里找到.

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

通过使用注释 SpringIntegrationTest 启动本地 Elasticsearch,并在所有测试完成后自动关闭.在内部,在设置集群、将信息获取到 JUnit 扩展以及启用 Spring 将相关信息自动装配到配置类方面做了相当多的工作.这个设置相当复杂,但最终它使用了与上面提到的相同的 Utils 类.

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

这篇关于如何使用 spring-data-elasticsearch 在 elasticsearch 中进行集成测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆