How to mock Solrclient request for java?

1.2k views Asked by At

I have a java code to establish a connection to the Solr server. I am not sure how I can mock it. Please check my code below.

SolrClient solrClient = new HttpSolrClient.Builder(url).build();
QueryResponse res = solrClient.query(solrQuery);
SolrDocumentList results = res.getResults();

Can someone please tell me how I can mock the above logic? Thanks.

1

There are 1 answers

1
Ahmad Abdelghany On BEST ANSWER

Well, it depends on what you are actually trying to test. You could do something like:

QueryResponse emptyResponse = new QueryResponse();
emptyResponse.setResponse(new NamedList<>(Map.of("response", new SolrDocumentList())));
when(solrClientMock.query(any())).thenReturn(emptyResponse);