Do you know how to propertly mock the Elasticsearch Java Client? Currently to mock the following request in Java:
SearchResponse response = client.prepareSearch(index)
.setTypes(type)
.setFrom(0).setSize(MAX_SIZE)
.execute()
.actionGet();
SearchHit[] hits = response.getHits().getHits();
I have to mock:
- client.prepareSearch
- SearchRequestBuilder:
- builder.execute
- builder.setSize
- builder.setFrom
- builder.setTypes
- SearchResponse:
- action.actionGet
- SearchResponse:
- response.getHits
- searchHits.getHits
So my test looks like:
SearchHit[] hits = ..........;
SearchHits searchHits = mock(SearchHits.class);
when(searchHits.getHits()).thenReturn(hits);
SearchResponse response = mock(SearchResponse.class);
when(response.getHits()).thenReturn(searchHits);
ListenableActionFuture<SearchResponse> action = mock(ListenableActionFuture.class);
when(action.actionGet()).thenReturn(response);
SearchRequestBuilder builder = mock(SearchRequestBuilder.class);
when(builder.setTypes(anyString())).thenReturn(builder);
when(builder.setFrom(anyInt())).thenReturn(builder);
when(builder.setSize(anyInt())).thenReturn(builder);
when(builder.execute()).thenReturn(action);
when(client.prepareSearch(index)).thenReturn(builder);
Ugly... So I would like to known if there is a more "elegant way" to mock this code.
Thanks
Aucun commentaire:
Enregistrer un commentaire