Resthighlevel client unit test is not working

I have written a method, below

    private RestHighLevelClient esClient;

    public SearchResponse getData(SearchRequest searchRequest) {
        try {
            SearchResponse searchResponse =, RequestOptions.DEFAULT);
  "Successfully fetched the data from ES");
            return searchResponse;
        } catch (ElasticsearchException e) {
            log.error("ES exception occurs on fetching data.");
        } catch (IOException e) {
            log.error("Runtime exception occurs on fetching data from ES.");

I have tried several way to write the unit test of it but sometime it gives null pointer exception, sometime can not mock a final class, final method etc. I have tried almost every way. So can anyone help me here by saying how I should write the unit test for this method.

You should use the Elasticsearch Java API Client [8.3] | Elastic.

I think you can mock the objects easily...

Otherwise have a look at:

It worked, thanks @dadoonet

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.