为基于spring-data-elasticsearch的应用程序编写集成测试

到目前为止,我们都知道,测试是我们作为软件工程师的工作的重要组成部分。 如果您或您的组织当前还不是这种情况,那么我不建议您尽快将其包含在开发工作流中。

我们还应该同意测试,尤其是集成测试有时可能具有挑战性或太复杂而无法组合在一起。 这可能就是为什么某些开发人员只是跳过了这一重要部分。

为了使事情变得不那么复杂和更易于访问,图书馆喜欢断言4j,模仿,春季测试,测试容器 have been created over the years. The easier it is to write tests,the more people will be willing to write them.

在我最近从事的项目中,弹性搜索通过春季数据,我们决定编写集成测试以验证这一点:

  • 我们的数据已在Elasticsearch服务器(ES)中正确索引,我们的搜索查询正在按预期运行并执行

为此,我们需要一种轻松地将新数据加载到测试中的方法ES,然后对其执行搜索查询。 我们已经有一个工具可以从我们的数据库中大量注入数据UAT环境,因此,如果我们能够以某种方式提取这些数据并将其用作提供测试数据的工具,那将是很好的ES。

After a few searches online, not finding a good tool for the job, I decided to create and publish my own take on the problem : spring-esdata-loader.

It is an open source Java 8+ testing library to help write integration tests for spring-data elasticsearch-based projects, by allowing you to easily load data into ES using simple annotations, leveraging Spring's entity mappings (i.e classes annotated with @Document, @Field, etc) and via specific JUnit 4's Rules or JUnit Jupiter's Extensions.

该库从实体类(索引名称,索引类型等)读取所需的所有元数据,使用它们创建/刷新相关索引,并提供ES使用数据ElasticsearchOperations已经存在于Spring的测试应用程序上下文中。

它提供以下现成的📦:

Features

  • 简单的API并且不需要配置支持JUnit4通过@LoadEsDataRule,@DeleteEsDataRule支持JUnit木星通过@LoadEsDataConfig/@LoadEsDataExtension@DeleteEsDataConfig/@DeleteEsDataExtensionBuilt-insupportforgzippeddataMultipledataformats(dump,manual)快速and内存效率高loadingofdataWritteninJava8BasedonSpring(Data,Test)

Installation & Usage

The library is split into 2 independent sub-modules, both are available on JCenter and Maven Central for download:

  • spring-esdata-loader-junit4用于测试JUnit 4春天的esdata加载器junit木星用于测试JUnit木星

因此,开始:

1.将适当的依赖项添加到您的摇动要么专家项目

JUnit 4JUnit木星摇篮依赖项{ testImplementation'com.github.spring-esdata-loader:spring-esdata-loader-junit4:VERSION' }依赖项{ testImplementation'com.github.spring-esdata-loader:spring-esdata-loader-junit-jupiter:VERSION' }马文<dependency>     <groupId>com.github.spring-esdata-loader</groupId>     <artifactId>spring-esdata-loader-junit4</artifactId>     <version>VERSION</version>     <scope>test</scope> </dependency><dependency>     <groupId>com.github.spring-esdata-loader</groupId>     <artifactId>spring-esdata-loader-junit-jupiter</artifactId>     <version>VERSION</version>     <scope>test</scope> </dependency>

2.编写测试类。

您可以看一下:

Supported Data Formats

弹簧数据加载器当前支持两种格式将数据加载到Elasticsearch中:倾倒和手册。

Dump data format

这是一个例子:

{"_index":"author","_type":"Author","_id":"1","_score":1,"_source":{"id":"1","firstName":"firstName1","lastName":"lastName1"}}
{"_index":"author","_type":"Author","_id":"2","_score":1,"_source":{"id":"2","firstName":"firstName2","lastName":"lastName2"}}
{"_index":"author","_type":"Author","_id":"3","_score":1,"_source":{"id":"3","firstName":"firstName3","lastName":"lastName3"}}
{"_index":"author","_type":"Author","_id":"4","_score":1,"_source":{"id":"4","firstName":"firstName4","lastName":"lastName4"}}
{"_index":"author","_type":"Author","_id":"5","_score":1,"_source":{"id":"5","firstName":"firstName5","lastName":"lastName5"}}
{"_index":"author","_type":"Author","_id":"6","_score":1,"_source":{"id":"6","firstName":"firstName6","lastName":"lastName6"}}
{"_index":"author","_type":"Author","_id":"7","_score":1,"_source":{"id":"7","firstName":"firstName7","lastName":"lastName7"}}
{"_index":"author","_type":"Author","_id":"8","_score":1,"_source":{"id":"8","firstName":"firstName8","lastName":"lastName8"}}
{"_index":"author","_type":"Author","_id":"9","_score":1,"_source":{"id":"9","firstName":"firstName9","lastName":"lastName9"}}
{"_index":"author","_type":"Author","_id":"10","_score":1,"_source":{"id":"10","firstName":"firstName10","lastName":"lastName10"}}

You can use a tool like elasticdump (requires NodeJS) to extract existing data
from your Elasticsearch server, and them dump them into a JSON file.

$ npx elasticdump --input=http://localhost:9200/my_index --output=my_index_data.json

The above command will run elasticdump to extract data from an index named my_index on a ES server located at http://localhost:9200 and then save the result into a file named my_index_data.json

If you change the --output part above into --output=$ | gzip my_data.json.gz the data will be automatically gzipped

Manual data format

使用这种格式,您可以直接指定目标数据(没有类似_指数,_资源,...),as an Array of JSON objects.

当您从头开始创建测试数据时(与从ES服务器转储现有数据相反),这更适合,因为以后更容易进行调整以适应将来的测试修改。

这是一个例子:

[
    {"id":"1","firstName":"firstName1","lastName":"lastName1"},
    {"id":"2","firstName":"firstName2","lastName":"lastName2"},
    {"id":"3","firstName":"firstName3","lastName":"lastName3"},
    {"id":"4","firstName":"firstName4","lastName":"lastName4"},
    {"id":"5","firstName":"firstName5","lastName":"lastName5"},
    {"id":"6","firstName":"firstName6","lastName":"lastName6"},
    {"id":"7","firstName":"firstName7","lastName":"lastName7"},
    {"id":"8","firstName":"firstName8","lastName":"lastName8"},
    {"id":"9","firstName":"firstName9","lastName":"lastName9"},
    {"id":"10","firstName":"firstName10","lastName":"lastName10"}
]

More complete examples can be found on the project's github under demo/.

快乐黑客Hack!

from: https://dev.to//tinesoft/writing-integration-tests-for-your-spring-data-elasticsearch-based-applications-3ec0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值