Ho una classe:Come testare una classe che usa HttpClient in Android usando il framework integrato?
public class WebReader implements IWebReader {
HttpClient client;
public WebReader() {
client = new DefaultHttpClient();
}
public WebReader(HttpClient httpClient) {
client = httpClient;
}
/**
* Reads the web resource at the specified path with the params given.
* @param path Path of the resource to be read.
* @param params Parameters needed to be transferred to the server using POST method.
* @param compression If it's needed to use compression. Default is <b>true</b>.
* @return <p>Returns the string got from the server. If there was an error downloading file,
* an empty string is returned, the information about the error is written to the log file.</p>
*/
public String readWebResource(String path, ArrayList<BasicNameValuePair> params, Boolean compression) {
HttpPost httpPost = new HttpPost(path);
String result = "";
if (compression)
httpPost.addHeader("Accept-Encoding", "gzip");
if (params.size() > 0){
try {
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
}
try {
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
if (entity.getContentEncoding() != null
&& "gzip".equalsIgnoreCase(entity.getContentEncoding()
.getValue()))
result = uncompressInputStream(content);
else
result = convertStreamToString(content);
} else {
Log.e(MyApp.class.toString(), "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
private String uncompressInputStream(InputStream inputStream)
throws IOException {...}
private String convertStreamToString(InputStream is) {...}
}
non riesco a trovare un modo per testare utilizzando un framework standard. Soprattutto, ho bisogno di simulare la perdita totale di Internet dall'interno del test.
ci sono suggerimenti per trasformare manualmente Internet nell'emulatore fuori durante l'esecuzione del test. Ma non mi sembra una buona soluzione, perché i test automatici dovrebbero essere ... automatici.
ho aggiunto un campo "client" per la classe cercando di prendere in giro dall'interno della classe di test. Ma l'implementazione dell'interfaccia HttpClient sembra abbastanza complessa.
Il framework Robolectric consente agli sviluppatori di testare Http connection per quanto ne so. Ma immagino che ci sia un modo per scrivere un test del genere senza usare un framework aggiuntivo così grande.
Quindi ci sono modi brevi e semplici delle classi di unit test che utilizzano HttpClient? Come hai risolto questo nei tuoi progetti?
Grazie per la risposta. Bene, il titolo dice sull'unità che collauda una classe che usa HttpClient, non intendevo testare lo stesso HttpClient. Scusa, se non l'avessi chiarito. L'opzione con il gestore WiFi è ottima, ma il telefono continua a connettersi tramite la rete mobile. Ho provato una modalità aereo, è ancora in connessione. Quindi, suppongo, Robolectric sia l'unica scelta. Grazie. –