如何將HttpURLConnection的嘲笑到的getContent()示例代碼的方法,還如何從模擬網址,以獲取效應初探Junit的:如何嘲笑HttpURLConnection的
public class WebClient {
public String getContent(URL url) {
StringBuffer content = new StringBuffer();
try {
HttpURLConnection connection = createHttpURLConnection(url);
connection.setDoInput(true);
InputStream is = connection.getInputStream();
int count;
while (-1 != (count = is.read())) {
content.append(new String(Character.toChars(count)));
}
} catch (IOException e) {
return null;
}
return content.toString();
}
protected HttpURLConnection createHttpURLConnection(URL url) throws IOException{
return (HttpURLConnection)url.openConnection();
}
}
感謝