How do I test a function which returns a complex object?

I’m trying to test a function in java, which helps me create a bool query in elasticsearch and returns a QueryBuilder object.

public QueryBuilder getBoolQueryForRequest(request) {      // .. bool query creation logic      return boolQuery; } 

The logic for query creation is complex and thus the object which can be asserted against the return value of this function is involved. How should I go about my testing?

This link talks about converting the QueryBuilder to a string query and then comparing, but again, the only way to test the original code is to use the original code to create the object, which is kind of a catch-22 for me..

Add Comment
1 Answer(s)

IHMO, writing unit tests for built Elastic queries don’t make really sense.
It is exactly as if you want to test a JPA query not against the response returned by the Database but against the text of the query.
How to ensure that it is correct when executed against your ElasticSearch DB and that it is valid in terms of syntax too ?

I think that similarly to DB queries, Elastic queries testing makes more sense as integration tests.
Unfortunately, I didn’t have the occasion to setup that in my Elastic devs where I worked on but you can get some feedback here and from that post, some ideas :

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

Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.