spring-boot - 在 Spring Boot 中使用不同的弹性指数进行测试
问题描述
我有一个使用 ElasticSearch 存储和索引产品的 Spring Boot 项目。产品型号为:
@Document(indexName = "products", type = "product")
public class Product {
@Id
public String id;
private String[] barcode;
private String name;
private String description;
private String image;
private String detail;
private double price;
private double gain;
private List<Category> categories;
private Currency currency;
private boolean eliminated;
private String taxDescription;
private double taxRate;
private int taxId;
}
我正在对产品的操作进行集成测试,例如搜索、更新等,我需要使用相同的弹性服务器。我正在考虑仅出于测试目的创建索引,用一些产品填充它,然后在测试后将其删除。可能吗?我怎样才能做到这一点?
解决方案
我假设您在生产代码中使用著名的Spring Data Repositories。像这样的东西可能存在于您的项目中:
@Repository
public interface ProductRepository extends ElasticsearchRepository<Product, String> {
Product findByName(String name);
}
因此,您可以设置一个小型集成测试用例,如下所示:
@SpringBootTest
@RunWith(SpringRunner.class)
public class QueryIndexIT {
@Autowired
private ProductRepository repo;
@Autowired
private ElasticsearchTemplate template;
@Test
public void queryRepo() {
assertThat(template.indexExists(Product.class)).isTrue();
Product savedProduct = repo.save(new Product(null, "the name", "n/a"));
Product foundProduct = repo.findByName("the name");
assertThat(foundProduct).isEqualTo(savedProduct);
}
}
}
所以基本上你正在使用你熟悉的 Spring 上下文运行这个集成测试,可能会通过application-test.properties
.
要独立于网络中任何现实生活中的 ElasticSearch 服务器,您还可以使用Maven Docker 插件将自己解耦。请特别注意我的片段executions
中下面列出的部分。pom.xml
它在任何集成测试运行之前启动一个新的 ElasticSearch 容器,并在测试终止后立即将其删除。因此,您以干净稳定的测试设置开始每个测试周期:
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.28.0</version>
<configuration>
<showLogs>true</showLogs>
<verbose>true</verbose>
<removeVolumes>true</removeVolumes>
<allContainers>true</allContainers>
<images>
<image>
<name>elasticsearch:6.6.2</name>
<alias>elasticsearch</alias>
<run>
<ports>
<port>9200:9200</port>
<port>9300:9300</port>
</ports>
<env>
<ES_JAVA_OPTS>-Xms512m -Xmx512m</ES_JAVA_OPTS>
<cluster.name>docker-cluster</cluster.name>
<discovery.type>single-node</discovery.type>
</env>
</run>
</image>
</images>
</configuration>
<executions>
<execution>
<id>prepare-containers</id>
<phase>pre-integration-test</phase>
<goals>
<goal>stop</goal>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>remove-containers</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
您可以在 Docker Hub 上的描述中找到有关引用的 ElasticSearch Docker 映像的更多信息。
所以你注意到了,没有必要仅仅为了测试目的而创建一个单独的索引。将测试和生产代码(以及可能的配置)紧密结合在一起,以避免在生产运行时出现意外。
如果您想解决设置中可能存在的问题,您还可以将您的方法与我在Github 存储库中为您准备的运行示例进行比较。
试试吧
mvn clean verify -Dit.test=QueryIndexIT
。玩得开心,带着你的进步或评论回来!
推荐阅读
- nginx - 根据标头的内容使用入口路由流量
- django - 如何在 Django 中正确使用 URL 映射以及初学者需要了解的内容
- javascript - 单击 YouTube 评论中的时间戳时不会触发 document.addEventListener
- java - kafka 连接启动失败(原因:java.lang.SecurityException)
- php - Magento 2 - 单击结帐按钮时“类不存在”
- python - 在 Python matplotlib 中绘制子图
- python - SQLAlchemy 多对多查询过滤器(标签系统)
- javascript - 我在从另一个组件传递的 Prop 中得到延迟响应?
- npm - npm 或纱线测试因“npm ERR!enoent spawn bash ENOENT”而失败
- python - 在 ubuntu 20.04 LTS 上使用 python Xlib 或 C-API 操作第二个(辅助)鼠标输入