我正在尝试在Java中测试一个函数,这可以帮助我在elasticsearch中创建 bool(boolean) 查询并返回QueryBuilder对象。
public QueryBuilder getBoolQueryForRequest(request) {
// .. bool query creation logic
return boolQuery;
}
创建查询的逻辑很复杂,因此涉及可以针对此函数的返回值声明的对象。我应该如何进行测试?这个link讨论了如何将QueryBuilder转换为字符串查询,然后进行比较,但是,再次测试原始代码的唯一方法是使用原始代码创建对象,这对我来说是个catch 22。
最佳答案
IHMO,为内置的 flex 查询编写单元测试没有任何意义。
就像您要针对数据库返回的响应而不是针对查询的文本来测试JPA查询一样。
如何确保在对您的ElasticSearch DB执行时它是正确的,并且在语法方面也有效?
我认为,与数据库查询类似, flex 查询测试作为集成测试更有意义。
不幸的是,我没有机会在我从事过的Elastic开发人员中进行设置,但是您可以从here和post中获得一些反馈:
Use the Gradle tools elasticsearch already has. You can read some information about this here: https://github.com/elastic/elasticsearch/issues/21119 620
Use the Maven plugin: https://github.com/alexcojocaru/elasticsearch-maven-plugin 785
Use Ant scripts like http://david.pilato.fr/blog/2016/10/18/elasticsearch-real-integration-tests-updated-for-ga
Using Docker: https://www.testcontainers.org/modules/elasticsearch
Using Docker from maven: https://github.com/dadoonet/fscrawler/blob/e15dddf72b1ed094dad279d492e4e0314f73683f/pom.xml#L241-L28992
关于java - 如何测试返回复杂对象的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62857237/