surya prakash
surya prakash

Reputation: 21

How to mock Solrclient request for java?

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.

Upvotes: 1

Views: 1414

Answers (1)

Ahmad Abdelghany
Ahmad Abdelghany

Reputation: 13248

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);

Upvotes: 0

Related Questions