java - 如何使用 spring-data-elasticsearch 在 elasticsearch 中进行集成测试?

标签 java elasticsearch integration-testing spring-data-elasticsearch elasticsearch-6.8

我正在使用 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 在 elasticsearch 中进行集成测试的任何其他选项吗?或者我应该如何为 elasticsearch 编写集成测试。

我知道在 stackoverflow 和其他门户网站上还有针对嵌入式 Elasticsearch 的其他答案,但这些答案不适用于我当前的 elasticsearch 版本。

最佳答案

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

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

对于 JUnit 5,您可能会查看当前 master 分支中的处理方式,相关类 are found here .

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

我希望这是一个好的起点

关于java - 如何使用 spring-data-elasticsearch 在 elasticsearch 中进行集成测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60435419/

相关文章:

grails 2.4.4 集成测试不使用测试数据源

elasticsearch - Elasticsearch中的结果过滤器和分页

java - Elasticsearch 6.4.2 的 Spring 数据不适用于 Java hashmap 属性

java - Java 7 中集成测试文件系统相关代码

java - 我如何使用 Java 解析 Wikiquotes 响应?

ruby - elasticsearch无法理解签名日期

scala - 在 Play2 for Scala 中测试 promises

java - 在 spring mvc 中获取 400 错误

ArrayList 和 RecyclerView 的 java.lang.OutOfMemoryError

java - 如何根据hadoop中的特定标准将数据存储在一个datanode上?