title: Mockito - Unable to initialize Spy on HttpEntity
body: <p>The code I'm testing works correctly, logs are right.</p>
  
  <hr>
  
  <p>Tests in error: 
    ConnectorTest: Unable to initialize @Spy annotated field 'entity'.</p>
  
  <hr>
  
  <ul>
  <li>Why can't I use verify on the http entity?</li>
  <li>Is there a better way to test it? Should I spy on the logs otherwise?</li>
  </ul>
  
  <p>CLASS TO BE TESTED:</p>
  
  <pre><code>public class Connector {
  
      private static final String hostname = "localhost";
      private int varnishPort = 8000;
  
      private int connectionTimeout = 5000; //millis
      private int requestTimeout = 5000;
      private int socketTimeout = 5000;
  
      private final HttpHost host;
      private HttpClient httpClient;
      private HttpEntity entity;
      private HttpResponse response;
  
      public Connector(){
  
          host = new HttpHost(this.hostname, this.varnishPort);
  
          RequestConfig.Builder requestBuilder = RequestConfig.custom();
          requestBuilder = requestBuilder.setConnectTimeout(connectionTimeout);
          requestBuilder = requestBuilder.setConnectionRequestTimeout(requestTimeout);
          requestBuilder = requestBuilder.setSocketTimeout(socketTimeout);
  
          HttpClientBuilder builder = HttpClientBuilder.create();     
          builder.setDefaultRequestConfig(requestBuilder.build());
          httpClient = builder.build();
      }
  
  
      public void invalidateVarnishCache( String level, String idDb ) {
  
          try{
              String xPurgeRegex = level+"/"+idDb+"$";
              Header header = new BasicHeader( "X-Purge-Regex", xPurgeRegex );
              BasicHttpRequest purgeRequest = new BasicHttpRequest("PURGE", "/" );
              purgeRequest.setHeader(header);
  
              response = httpClient.execute(host, purgeRequest);
  
              entity = response.getEntity();
  
              int statusCode = response.getStatusLine().getStatusCode();
  
              if( statusCode &gt;= 300 ){
  
                  int respLength = entity.getContent().available();
                  byte[] errorResp = new byte[ respLength ];
                  entity.getContent().read(errorResp, 0, respLength);
                  // log error
              }else{
                  // log success
              }
  
              EntityUtils.consume(entity);
  
          }catch(Exception e){
              // log exception
          }
      }
  
  }
  </code></pre>
  
  <p>MY TEST:</p>
  
  <pre><code>@RunWith(MockitoJUnitRunner.class)
  public class ConnectorTest {
  
      @Mock
      private HttpClient httpClient;
  
      // @Spy
      // private HttpEntity entity;
  
      @InjectMocks
      private Connector varnishPurger = new Connector();
  
  
      @Test
      public void purgeFail() throws Exception{
  
          HttpResponse response500 = new BasicHttpResponse( new ProtocolVersion( "HTTP/1.1", 0, 0), 500, "NO!_TEST" );
          HttpEntity entity500 = new StringEntity("BAD entity. [test]");
          response500.setEntity(entity500);
  
          doReturn( response500 )
          .when( httpClient ).execute( isA(HttpHost.class), isA(BasicHttpRequest.class) );
  
          varnishPurger.invalidateVarnishCache("level_test_500", "id_test_500");
  
          // verify( entity, times( 1 ) ).getContent().available();  // HOW DO I MAKE THIS WORK?
      }
  
      ...
  
  }
  </code></pre>