1

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

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

    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 进行集成测试的任何其他选项,或者我应该如何为弹性搜索编写集成测试。

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

4

1 回答 1

2

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

对于 JUnit 4,您可以检查使用Utils 类设置本地运行的 Elasticsearch 节点并在最后将其拆除的JUnit 4 规则。

对于 JUnit 5,您可能会看看在当前主分支中是如何处理的,相关类在此处找到

通过使用注解SpringIntegrationTest,本地 Elasticsearch 将启动并在所有测试完成后自动关闭。在内部,在设置集群、将信息放入 JUnit 扩展和启用 Spring 将相关信息自动装配到配置类中完成了相当多的工作。这个设置非常复杂,但最终它使用了Utils上面提到的相同类。

我希望这是一个好的起点

于 2020-02-27T19:20:07.007 回答