I am very new to unit testing using mocks.
So, I have a java method that build search request and call elasticsearch to do the searching. I am not sure how to unit test this method. In the code below should I mock "client.search()"? I think I should not because the result of client.search() is dependant on my code (the parameters I provide to it). So, I should not mock the output of client.search(), right?
How should I proceed with unit testing my method? Any tips will be greatly appreciated. Thank you.
public SearchResponse doV1Search(SearchRequestV1 searchRequestBody) throws IOException {
Date startDate = Date.from(searchRequestBody.getRange().getStartTime());
Date endDate = Date.from(searchRequestBody.getRange().getEndTime());
int size = ELASTICSEARCH_MAX_RECORDS_PER_RESPONSE;
int from = 0;
if (searchRequestBody.getMeta() != null) {
if (searchRequestBody.getMeta().getSize() != null
&& searchRequestBody.getMeta().getSize() != 0
&& searchRequestBody.getMeta().getSize() <= ELASTICSEARCH_MAX_RECORDS_PER_RESPONSE) {
size = Math.toIntExact(searchRequestBody.getMeta().getSize());
}
if (searchRequestBody.getMeta().getFrom() != null) {
from = Math.toIntExact(searchRequestBody.getMeta().getFrom());
}
}
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.from(from);
searchSourceBuilder.size(size);
searchSourceBuilder.sort(new FieldSortBuilder("creationTimestamp").order(SortOrder.DESC));
searchSourceBuilder.query(
buildQuery(
startDate,
endDate,
ModelMappings.convertV1SearchRequestFieldsToV3(searchRequestBody.getFields())
)
);
SearchRequest searchRequest = new SearchRequest(buildValidIndicesFromDateRange(startDate, endDate));
searchRequest.indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN);
searchRequest.source(searchSourceBuilder);
return client.search(searchRequest, RequestOptions.DEFAULT);
}
Aucun commentaire:
Enregistrer un commentaire