我有从网站获取 HTML 的 Java 程序。它在控制台上显示内容,然后将其保存到名为web_content.txt
. 我该如何为此编写测试用例?
我的程序是:
public class UrlDown {
public static void main(String[] args) throws Exception {
UrlDown down = new UrlDown();
File f = new File("web_content.txt");
String loc = "http://www.google.com";
down.saveUrlToFile(f, loc);
}
public void saveUrlToFile(File saveFile, String location) {
URL url;
try {
url = new URL(location);
BufferedReader in = new BufferedReader(new InputStreamReader(
url.openStream()));
BufferedWriter out = new BufferedWriter(new FileWriter(saveFile));
char[] cbuf = new char[255];
StringBuilder builder = new StringBuilder();
while ((in.read(cbuf)) != -1) {
out.write(cbuf);
builder.append(cbuf);
}
String downloaded = builder.toString();
System.out.println();
System.out.println(downloaded);
in.close();
out.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}