Are there any examples of how to mock an Elasticsearch search result for the Java API for unit tests with Mockito? Do you mock the entire search result or individual hits? If I search the Internet for examples I only see the old Rest Client examples. Can you use an input stream to manually create your search response?
Someone has previously asked here, but there was no answer to their question. There are no examples that show how to create a manual SearchResponse<> that I can find.
Surely somebody has done this before. It was not that difficult to do in the old REST client, but the SearchResponse structure is different now. I've tried something like:
ElasticsearchClient client = mock(ElasticsearchClient.class);
// Set up mocked results
var response = new SearchResponse.Builder().build().hits().hits().add(0,
new ESMetrics("aln-nbadev4.labs.server.com", NAME_FIELD_VALUE, SPARK_MASTER_VALUE,
"aln-nbadev4.labs.server.com", 0L));
However, I get: "MissingRequiredProperty 'SearchResponse.took'". The idea is to mock in a way similar to this:
It is also very important when defining your class that holds the Elasticsearch data that you are reading that if you are using Lombock notations you must use @NoArgsConstructor if any constructors are defined:
@Data
@RequiredArgsConstructor
@NoArgsConstructor(force=true)
@JsonIgnoreProperties(ignoreUnknown = true)
public static class ESMetrics {
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.