title: How to mock an Elasticsearch Java Client?
body: <p>Do you know how to propertly mock the Elasticsearch Java Client? Currently to mock the following request in Java:</p>
  
  <pre><code>SearchResponse response = client.prepareSearch(index)
                  .setTypes(type)
                  .setFrom(0).setSize(MAX_SIZE)
                  .execute()
                  .actionGet();
  SearchHit[] hits = response.getHits().getHits();
  </code></pre>
  
  <p>I have to mock:</p>
  
  <ul>
  <li>client.prepareSearch</li>
  <li><strong>SearchRequestBuilder:</strong>
  <ul>
  <li>builder.execute</li>
  <li>builder.setSize</li>
  <li>builder.setFrom</li>
  <li>builder.setTypes</li>
  </ul></li>
  <li><strong>SearchResponse:</strong>
  <ul>
  <li>action.actionGet</li>
  </ul></li>
  <li><strong>SearchResponse:</strong>
  <ul>
  <li>response.getHits</li>
  <li>searchHits.getHits</li>
  </ul></li>
  </ul>
  
  <p>So my test looks like:</p>
  
  <pre><code>SearchHit[] hits = ..........;
  
  SearchHits searchHits = mock(SearchHits.class);
  when(searchHits.getHits()).thenReturn(hits);
  
  SearchResponse response = mock(SearchResponse.class);
  when(response.getHits()).thenReturn(searchHits);
  
  ListenableActionFuture&lt;SearchResponse&gt; 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);
  </code></pre>
  
  <p>Ugly...
  So I would like to known if there is a more "elegant way" to mock this code.</p>
  
  <p>Thanks</p>