vendredi 27 février 2015

How to properly clean up after using an HttpClient in a unit test

In a unit test using an Apache HttpClient to fire requests, I have seen the following setup and cleanup code:



private HttpClient httpClient;
private HttpRequestBase httpRequest;


@Before
public void setUp() throws Exception {
httpClient = new DefaultHttpClient();
}

@After
public void closeRequests() {
if (httpRequest != null) {
httpRequest.releaseConnection();
httpRequest = null;
}
}


The tests than e.g. send get requests and check the response:



@Test
public void getSomething() throws Exception {
httpGet = new HttpGet("http://some/url");
HttpResponse response = httpclient.execute(httpGet);
assertThat(response.getStatusLine().getStatusCode(), is(HttpStatus.SC_OK));
}


Now my question is: Do these tests properly clean up after themselves? From what I understand, the releaseConnection() call only hands back the connections to the client's connection manager but doesn't actually close it.


So shouldn't the tests rather do this:



@After
public void closeConnections() {
httpClient.getConnectionManager().shutdown();
}


And would this properly close all connections even without calling releaseConnection() on the http request instances?


Aucun commentaire:

Enregistrer un commentaire